convert date format YYYYMMDD to MM/DD/YYYY | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

convert date format YYYYMMDD to MM/DD/YYYY

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 06-07-2006
nasirgondal nasirgondal is offline
Registered User
 
Join Date: Jun 2006
Last Activity: 23 February 2007, 12:03 PM EST
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
convert date format YYYYMMDD to MM/DD/YYYY

In my shell script i have a variable which stores date in the format of YYYYMMDD. Is there any way to format this value to MM/DD/YYYY.

Thanks.
Sponsored Links
    #2  
Old 06-08-2006
tayyabq8's Avatar
tayyabq8 tayyabq8 is offline Forum Advisor  
Ex-Moderator
 
Join Date: Nov 2004
Last Activity: 16 December 2013, 12:55 AM EST
Location: Bahrain
Posts: 592
Thanks: 3
Thanked 8 Times in 3 Posts

Code:
echo "YYYYMMDD" | awk '
BEGIN {OFS="/"}
{print substr($1,5,2), substr($1,7,2), substr($1,1,4)}'

Regards,
Tayyab
Sponsored Links
    #3  
Old 06-08-2006
vino's Avatar
vino vino is offline Forum Advisor  
Supporter (in vino veritas)
 
Join Date: Feb 2005
Last Activity: 9 April 2013, 8:19 AM EDT
Location: Sydney, Down Under
Posts: 2,847
Thanks: 0
Thanked 12 Times in 12 Posts
Quote:
Originally Posted by nasirgondal
In my shell script i have a variable which stores date in the format of YYYYMMDD. Is there any way to format this value to MM/DD/YYYY.

Thanks.
You have broken a rule by duplicating your thread. The other one is here - changing the format of date.

Using shell script.

Code:
#! /bin/ksh

format=YYYYMMDD

YEAR=${format%????}
DAY=${format#??????}
MON=${format#$YEAR}
MON=${MON%$DAY}
echo $MON/$DAY/$YEAR

Using sed.

Code:
echo "YYYYMMDD" | sed -n -e "s_\(....\)\(..\)\(..\)_\2/\3/\1_p"

    #4  
Old 06-08-2006
nasirgondal nasirgondal is offline
Registered User
 
Join Date: Jun 2006
Last Activity: 23 February 2007, 12:03 PM EST
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
It worked!

Thank you.
Sponsored Links
    #5  
Old 06-13-2006
arunkumar_mca arunkumar_mca is offline
Registered User
 
Join Date: Oct 2004
Last Activity: 18 September 2014, 6:05 AM EDT
Posts: 281
Thanks: 1
Thanked 1 Time in 1 Post
hi all....

Want some clarification ..

In the below scripts ...

echo "YYYYMMDD" | awk 'BEGIN {OFS="/"}{print substr($1,5,2), substr($1,7,2), substr($1,1,4)}'

What is OFS..Also how does the '/' is placed inbetween..

Thanks in advance,
Arun....
Sponsored Links
    #6  
Old 06-13-2006
tayyabq8's Avatar
tayyabq8 tayyabq8 is offline Forum Advisor  
Ex-Moderator
 
Join Date: Nov 2004
Last Activity: 16 December 2013, 12:55 AM EST
Location: Bahrain
Posts: 592
Thanks: 3
Thanked 8 Times in 3 Posts
OFS is Output Field Separator, I don't know the internal technicle details of awk though I can imagine that awk puts each print command in a stack like

Code:
print substr($1,5,2)
print substr($1,7,2)
print substr($1,1,4)

and then separates each output of print with an OFS if defined, default OFS is " " a whitespace. Hope it helps.

Regards,
Tayyab

Last edited by tayyabq8; 06-13-2006 at 10:54 AM..
Sponsored Links
    #7  
Old 08-07-2008
rspk_praveen rspk_praveen is offline
Registered User
 
Join Date: Aug 2008
Last Activity: 29 June 2009, 8:55 AM EDT
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
hi nasirgondal,

I also wanted such a script. It works fine but if the date is not exactly in YYYYMMDD format, lets say YYYYMDD or YYYYMMD format its not working. Is there any solution even for this. Appreciate your help

#! /bin/ksh

format=YYYYMMDD

YEAR=${format%????}
DAY=${format#??????}
MON=${format#$YEAR}
MON=${MON%$DAY}
echo $MON/$DAY/$YEAR
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Converting Date from YYYYMMDD to DD-MON-YYYY ady_koolz Shell Programming and Scripting 17 12-15-2011 12:23 PM
change date format from yyyy/mm/dd to dd/mm/yyyy keyboarder Shell Programming and Scripting 6 11-10-2010 04:58 AM
PERL String to Date (Custom format yyyymmdd to dd-mon-yyyy) guruparan18 Shell Programming and Scripting 9 03-05-2010 05:47 AM
Format date from MM/DD/YYYY to YYYYMMDD ChicagoBlues UNIX for Dummies Questions & Answers 5 03-24-2009 07:10 PM
how to convert the string YYYYMMDD into YYYY.MM.DD spatra UNIX for Dummies Questions & Answers 1 07-23-2008 07:18 AM



All times are GMT -4. The time now is 01:49 AM.