Formatting dates to a specific pattern

prad prad at
Sun Aug 31 01:47:12 UTC 2008

On Sun, 31 Aug 2008 02:52:07 +0300
"Ivan \"Rambius\" Ivanov" <rambiusparkisanius at> wrote:

> I need to format the current date (as returned by date(1) ) to the
> pattern m-d-yyyy, where m is the month in one or digits, d is the day
> in one or two digits, and yyyy is the year in four digits. The problem
> for me is the day and the month, for example August should be 8, and
> not 08, and 5th of September should be 9-5-2008 and not 09-05-2008.
hello rambius!

you can give this script a try - it seems to do what you want and
has comments too. save it as, chmod +x it and run it as 
./ `date "+%m-%d-%Y"`

(there are no doubt better ways to do what you want especially if you
use a more advanced shell like zsh, but this may be sufficient)

# removes 0 from mm-dd-yyyy
# run with ./ `date "+%m-%d-%Y"`

#the whole date from argument $1

#get the year

#get the month and day

#get the day

#get the month

#remove 0 if only at beginning of month, day and add on the year
echo ${mm#0}-${dd#0}-$yyyy

In friendship,

                                      ... with you on your journey
Towards Freedom (website)
Information, Inspiration, Imagination - truly a site for soaring I's

More information about the freebsd-questions mailing list