Formatting dates to a specific pattern

perryh at pluto.rain.com perryh at pluto.rain.com
Sun Aug 31 03:48:37 UTC 2008


> > I need to format the current date ... to the pattern
> > m-d-yyyy ... date(1) seems to always put leading zeros.
>
> # date "+%m-%d-%Y" | sed 's/^0//g'
> 8-30-2008

Not quite.  That fixes the month, but not the day:

$ echo 02-04-2008 | sed 's/^0//g'
2-04-2008

(The g does nothing, because the ^ can match only at the
beginning of a line.)  This does both:

$ echo 02-04-2008 | sed -e 's/^0//' -e 's/-0*/-/'
2-4-2008


More information about the freebsd-questions mailing list