Converting Date from YYYYMMDD to DD-MON-YYYY

Hi , I need to convert date from YYYYMMDD to DD-MON-YYYY

e.g
20111214 to 14-Dec-2011

Please help.

This might help ..

here is your code.. :slight_smile:

date=20111214
len=${#date}
echo $len
dat=${date:6:8}
yr=${date:0:4}
mon=${date:4:6}
mon=${mon:0:2}
var1="jan"
var2="feb"
var3="mar"
var4="apr"
var5="may"
var6="jun"
var7="jul"
var8="aug"
var9="sep"
var10="oct"
var11="nov"
var12="dec"
val=$( eval eval echo \$var$mon )
echo "$dat-$val-$yr"
$ date --date="20111214" +"%d-%b-%Y"
14-Dec-2011

or

$ echo "20111214" > my_dates_file.txt
$ date -f my_dates_file.txt +"%d-%b-%Y"
14-Dec-2011

@vivek ... Thanks but its not working . It is giving bad substitution error.
I am a novice to scripting . Also I am using as Sun Solaris unix machine

---------- Post updated at 03:01 AM ---------- Previous update was at 02:59 AM ----------

@ni2 date command --date and -f options are not available on sun solaris machine

@ady_koolz: my code is tested it works fine in linux.. i donno about solaris... but anyway try this once.. comment this line
#mon=${mon:0:2}

and try.. hope it works...

---------- Post updated at 01:50 PM ---------- Previous update was at 01:46 PM ----------

@ni2: your code is best dude... its damn simple :slight_smile:

@vivek its still not working on solaris....anywayz thanx...

Try this...

echo "20111214" | nawk 'BEGIN{a[1]="JAN";a[2]="FEB";a[11]="NOV";a[12]="DEC"}
{match($0,"(....)(..)(..)",d);print d[3],a[d[2]],d[1]}' OFS="-"

of course, fill in the remaining months!
--ahamed

@ady_koolz
Sorry. Can't help with that.

@vivek d r
Thanks.

 echo "20111214" | nawk 'BEGIN{split("JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC",month," ")}{printf("%s-%s-%s\n",substr($0,7,2),month[substr($0,5,2)],substr($0,1,4))}'

@itikamraj and ahamed

It is half working . If the date come as 20110612 then it does not show the month.Because I think month array does not recoznize 06.

Try this...

echo "20111214" | nawk 'BEGIN{a[1]="JAN";a[2]="FEB";a[11]="NOV";a[12]="DEC"}
{match($0,"(....)(..)(..)",d);print d[3],a[d[2]+0],d[1]}' OFS="-"

--ahamed

@ady_koolz: Try the below perl program. It'll even check if input date is in YYYYMMDD format.
[highlight=perl]
#! /usr/bin/perl -w
use strict;

(@ARGV != 1) && die "Provide only one parameter in YYYYMMDD format. Exiting";

my ($yr, $mt, $dt) = (0, 0, 0);

if ($ARGV[0] =~ /20[0-9]{2}(0[1-9]|1[0-2])(0[1-9]|[12][0-9]|3[01])/) {
$yr = substr $ARGV[0], 0, 4;
$mt = substr $ARGV[0], 4, 2;
$dt = substr $ARGV[0], 6, 2;
}
else {
die "Improper format. Exiting";
}

SWITCH: {
$mt == "01" && do { $mt = "Jan"; last SWITCH };
$mt == "02" && do { $mt = "Feb"; last SWITCH };
$mt == "03" && do { $mt = "Mar"; last SWITCH };
$mt == "04" && do { $mt = "Apr"; last SWITCH };
$mt == "05" && do { $mt = "May"; last SWITCH };
$mt == "06" && do { $mt = "Jun"; last SWITCH };
$mt == "07" && do { $mt = "Jul"; last SWITCH };
$mt == "08" && do { $mt = "Aug"; last SWITCH };
$mt == "09" && do { $mt = "Sep"; last SWITCH };
$mt == "10" && do { $mt = "Oct"; last SWITCH };
$mt == "11" && do { $mt = "Nov"; last SWITCH };
$mt == "12" && do { $mt = "Dec"; last SWITCH };
}

print "$ARGV[0] is $dt-$mt-$yr\n";[/highlight]

[root@hostname test]# ./test.pl 20111231
20111231 is 31-Dec-2011

@ahamed , Your code is giving syntax error.I am using SunOS...match function is not working there.

itkamaraj's code modified... try this...

echo "20110614" | nawk 'BEGIN{split("JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC",month," ")}
{printf("%s-%s-%s\n",substr($0,7,2),month[substr($0,5,2)+0],substr($0,1,4))}'

--ahamed

1 Like

@ahamed..

Thanks a lot bro...It is working :slight_smile:

x=20111214
m=JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC
echo "${x:6:2}-${m:$((10#${x:4:2}*3-3)):3}-${x:0:4}"

If you have ksh93 then you can do same as GNU date.

printf "%(%d-%b-%Y)T" "20111214"

Download ksh93 for Solaris.