Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 30 Aug 2008 18:47:10 -0700
From:      prad <prad@towardsfreedom.com>
To:        freebsd-questions@freebsd.org
Subject:   Re: Formatting dates to a specific pattern
Message-ID:  <20080830184710.2a618fad@gom.home>
In-Reply-To: <89ce7f740808301652g169fa4d2v3fe5eff06100e31e@mail.gmail.com>
References:  <89ce7f740808301652g169fa4d2v3fe5eff06100e31e@mail.gmail.com>

next in thread | previous in thread | raw e-mail | index | archive | help
On Sun, 31 Aug 2008 02:52:07 +0300
"Ivan \"Rambius\" Ivanov" <rambiusparkisanius@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



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20080830184710.2a618fad>