Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

handling date field

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 04-03-2007
mgirinath mgirinath is offline
Registered User
 
Join Date: May 2005
Last Activity: 3 November 2008, 2:18 PM EST
Posts: 69
Thanks: 0
Thanked 0 Times in 0 Posts
handling date field

Hi,

Is there any way we could change the date format listed below...

date I get is 03302007 (MMDDYYYY)
I need to change it to 20070330 (YYYYMMDD)

Thanks.
Sponsored Links
    #2  
Old Unix and Linux 04-03-2007
vgersh99's Unix or Linux Image
vgersh99 vgersh99 is offline Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 23 February 2017, 12:55 AM EST
Location: Foxborough, MA
Posts: 8,046
Thanks: 205
Thanked 733 Times in 684 Posts

Code:
echo '03302007' | sed 's#\(..\)\(..\)\(....\)#\3\1\2#'

Sponsored Links
    #3  
Old Unix and Linux 04-03-2007
tumblez tumblez is offline
Registered User
 
Join Date: Nov 2002
Last Activity: 22 September 2011, 3:24 AM EDT
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
If you are wanting the current system date in that format...

date +"%Y%M%d"

will produce...

20070903
    #4  
Old Unix and Linux 04-03-2007
cfajohnson's Unix or Linux Image
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
 
Join Date: Mar 2007
Last Activity: 11 July 2016, 2:55 PM EDT
Location: Toronto, Canada
Posts: 2,898
Thanks: 0
Thanked 134 Times in 118 Posts
Quote:
Originally Posted by mgirinath
Is there any way we could change the date format listed below...

date I get is 03302007 (MMDDYYYY)
I need to change it to 20070330 (YYYYMMDD)

Separate the components, then put them back together in the order you want:


Code:
date=03302007
temp=${date#??}
month=${date%"$temp"}
day=${temp%????}
year=${temp#??}
newdate=$year$month$day

Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Check if a date field has date or timestamp or date&timestamp machomaddy Shell Programming and Scripting 1 02-25-2012 11:29 PM
Sorting Date Field with Sort -k :/ Glitch100 Shell Programming and Scripting 17 01-05-2012 01:09 PM
date field manipulation dsravan Shell Programming and Scripting 5 06-23-2010 03:58 AM
Arrange date in a field sabercats Shell Programming and Scripting 6 02-28-2006 04:56 PM
Date Handling AkumaTay UNIX for Dummies Questions & Answers 1 08-16-2001 03:53 AM



All times are GMT -4. The time now is 03:04 AM.