Change Date Format

Hi Guys,

I had a scenario like this..
It seems very silly...dont think it as a home work question.....:slight_smile:

i tried it many ways but i didn't achieve this...

start_date=May122011

here i want to change the start_date in to 20110512

start_date=20110512

tell me how can we achive this.....:slight_smile:

It seems very silly
thanks&regards,
venkat.

Hi,

you'll probably find what you're after in the FAQ: http://www.unix.com/answers-frequently-asked-questions/13785-yesterdays-date-date-arithmetic.html

but in case you've already looked, what have you tried already, and what OS are you using ?

You can do something like that :

start_date=May122011
start_date=$( nawk -v Date=${start_date} '
                BEGIN { Months="   JanFebMarApeMayJunJulSepOctNovDec"
                        year  = substr(Date, 6, 4) + 0;
                        day   = substr(Date, 4, 2) + 0;
                        month = index(Months, substr(Date, 1, 3)) / 3;
                        printf "%04d%02d%02d", year, month, day;
                        exit;
                      }
              '
            )

Jean-Pierre.

Hi,

Thanks for your reply..

The script u have gine is not working..
i'm not familiar with AWk can u please explain the script..

i'm using solaris 10..

:slight_smile:

Please give us more infos about the error.
Works fine for me (on Solaris 10) :

$ uname -a
SunOS xxxxxxxx 5.10 Generic_120011-14 sun4u sparc SUNW,Sun-Fire-V440
$ start_date=May122011
$ start_date=$( nawk -v Date=${start_date} '
Shell>                 BEGIN { Months="   JanFebMarApeMayJunJulSepOctNovDec"
Shell>                         year  = substr(Date, 6, 4) + 0;
Shell>                         day   = substr(Date, 4, 2) + 0;
Shell>                         month = index(Months, substr(Date, 1, 3)) / 3;
Shell>                         printf "%04d%02d%02d", year, month, day;
Shell>                         exit;
Shell>                       }
Shell>               '
Shell>             )
$ echo $start_date
20110512
$

Hi,

I got a problem here..

month = index(Months, substr(Date, 1, 3)) this lcommand is giving the value as 13

so the output in the script is vary...

i found the following solution for this but it is just as managable..

#! /bin/sh
start_date=May122011
 New_date=` nawk -v Date=${start_date} '
 BEGIN { Months="JanFebMarAprMayJunJulAugSepOctNovDec"
 year = substr(Date, 6, 4) + 0;
 day = substr(Date, 4, 2) + 0;
 month = (index(Months, substr(Date, 1, 3))+2)/3;
 printf "%04d%02d%02d", year, month, day;
 exit;
}
'`
 echo $New_date

Plz check this. Correct me if any thing wrong..

Thanks & regards,
venkat. :slight_smile:
:slight_smile:

---------- Post updated at 07:48 AM ---------- Previous update was at 07:45 AM ----------

Hi,
sorry for the previous reply..
i understood your script..

i unfortunately removed the spaces before months..
now i got it..

good work man..

thanq very much... :slight_smile: