Computing.Net > Forums > Unix > Date Conversion from 'DD-MON-YY' to

Date Conversion from 'DD-MON-YY' to

Reply to Message Icon

Original Message
Name: covinadr
Date: July 31, 2008 at 12:39:43 Pacific
Subject: Date Conversion from 'DD-MON-YY' to
OS: AIX
CPU/Ram: 512
Model/Manufacturer: AIX
Comment:

I want to convert date passed as a variable in the format
'DD-MON-YY' i.e., '31-JUL-08' to 'YYYYMMDD' format
'20080731' . Someone gimme a single line command. Anybody here, help...!

TIA

TIA
Covina


Report Offensive Message For Removal


Response Number 1
Name: covinadr
Date: July 31, 2008 at 13:30:35 Pacific
Subject: Date Conversion from 'DD-MON-YY' to
Reply: (edit)

If someone can post from the otherway, it's fine with me, i.e., convert from 'YYYYMMDD' to 'DD-MON-YY'.

TIA
Covina


Report Offensive Follow Up For Removal

Response Number 2
Name: nails
Date: August 1, 2008 at 11:47:12 Pacific
Subject: Date Conversion from 'DD-MON-YY' to
Reply: (edit)

You'll never do this in one line because of having to convert the year string to a number:


#!/bin/ksh

# This korn shell function receives a date of the format
# 01-AUG-08 and returns a string YYYYMMDD
function yrmmdd
{
typeset -2Z mm
typeset -2Z yr

# parse the argument into $1, $2, $3
set - $(IFS="-"; echo $1)

yr=$3
dd=$1

case $2 in
"JAN") mm=1;;
"FEB") mm=2;;
"MAR") mm=3;;
"APR") mm=4;;
"MAY") mm=5;;
"JUN") mm=6;;
"JUL") mm=7;;
"AUG") mm=8;;
"SEP") mm=9;;
"OCT") mm=10;;
"NOV") mm=11;;
"DEC") mm=12;;
*) echo "error"
exit ;;
esac

echo "20"${yr}${mm}${dd}
} # end function


dstr=01-AUG-08

retstr=$(yrmmdd $dstr)
echo $retstr

In fact, I answered a similar question for you here:

http://www.computing.net/answers/un...


Report Offensive Follow Up For Removal

Response Number 3
Name: fpmurphy
Date: August 10, 2008 at 14:48:23 Pacific
Subject: Date Conversion from 'DD-MON-YY' to
Reply: (edit)

Using ksh93, I can be done as a one-liner.

$ printf "%(%Y%m%d)T\n" "31-JUL-2008"
20080731


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Date Conversion from 'DD-MON-YY' to

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




How often do you use Computing.Net?

Every Day
Once a Week
Once a Month
This Is My First Time!


View Results

Poll Finishes In 2 Days.
Discuss in The Lounge