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

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Click here to start participating now! Also, check out the New User Guide.

Date Conversion from 'DD-MON-YY' to

Reply to Message Icon

Name: covinadr
Date: July 31, 2008 at 12:39:43 Pacific
OS: AIX
CPU/Ram: 512
Product: 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



Sponsored Link
Ads by Google

Response Number 1
Name: covinadr
Date: July 31, 2008 at 13:30:35 Pacific
Reply:

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

TIA
Covina


0

Response Number 2
Name: nails
Date: August 1, 2008 at 11:47:12 Pacific
Reply:

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...


0

Response Number 3
Name: fpmurphy
Date: August 10, 2008 at 14:48:23 Pacific
Reply:

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

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


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


How to identify end of li... Freebsd /etc/rc.d/hostnam...



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Date Conversion from 'DD-MON-YY' to

retrieve DD/MM/YY HH:MI file information www.computing.net/answers/unix/retrieve-ddmmyy-hhmi-file-information/2761.html

Convert Date format www.computing.net/answers/unix/convert-date-format-/8158.html

set variables from date conversion www.computing.net/answers/unix/set-variables-from-date-conversion/6682.html