Formatting dates to a specific pattern

prad prad at towardsfreedom.com
Sun Aug 31 01:47:12 UTC 2008


On Sun, 31 Aug 2008 02:52:07 +0300
"Ivan \"Rambius\" Ivanov" <rambiusparkisanius at gmail.com> 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 de0.sh, chmod +x it and run it as 
./de0.sh `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)

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

#the whole date from argument $1
mmddyyyy=$1

#get the year
yyyy=${mmddyyyy##*-}

#get the month and day
mmdd=${mmddyyyy%-*}

#get the day
dd=${mmdd#*-}

#get the month
mm=${mmdd%-*}

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


-- 
In friendship,
prad

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


More information about the freebsd-questions mailing list