Computing.Net > Forums > Unix > Convert Date format

Convert Date format

Reply to Message Icon

Original Message
Name: covinadr
Date: June 16, 2008 at 12:41:04 Pacific
Subject: Convert Date format
OS: AIX
CPU/Ram: 512
Model/Manufacturer: HP
Comment:

I send parameters in the following format of the date, it will always be the last date of the month in the format DD-MON-YY ex: '31-JAN-08','30-APR-08'. I need to convert this 200801 and 200804. I need a simple easy shell script that can do this.

Any help will be appreciated

TIA
Covina


Report Offensive Message For Removal


Response Number 1
Name: nails
Date: June 16, 2008 at 21:52:27 Pacific
Subject: Convert Date format
Reply: (edit)


#!/bin/ksh


# This korn shell function receives a date of the format
# 31-JUN-08 and returns a string YYYYMo
function yrmon_string
{
typeset -2Z mm
typeset -2Z yr

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

yr=$3

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}
} # end function

dstr=31-FEB-09

retstr=$(yrmon_string $dstr)
echo $retstr


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: Convert Date format 

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