LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 06-07-2011, 12:45 AM   #1
hattori.hanzo
Member
 
Registered: Aug 2006
Posts: 167

Rep: Reputation: 15
Convert from DD/MM/YYYY to YYYYMMDD


Hello,

How does one convert from DD/MM/YYYY to YYYYMMDD?

Code:
06/06/2011 
06/06/2011
to

Code:
20110606
20110606
Thanks & Regards
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 06-07-2011, 01:10 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
Details? In the shell? In a programming language of some kind? How?

Using the shell and the gnu date command, this:
Code:
date -d 06/06/2011 +%Y%m%d
 
1 members found this post helpful.
Old 06-07-2011, 01:15 AM   #3
hattori.hanzo
Member
 
Registered: Aug 2006
Posts: 167

Original Poster
Rep: Reputation: 15
Thanks for responding.

In a bash script. I need to process a couple of thousand lines on a scheduled basis which I can schedule via cron. Just need to figure out how to get the date in the correct format.

I am able to convert YYYYMMDD to DD/MM/YYYY but not figured out how to reverse it (DD/MM/YYYY to YYYYMMDD).
 
Old 06-07-2011, 01:25 AM   #4
micxz
Senior Member
 
Registered: Oct 2002
Location: CA
Distribution: openSuSE, Cent OS, Slackware
Posts: 1,131

Rep: Reputation: 75
How about:
Code:
awk -F / '{print $3$2$1}'

Last edited by micxz; 06-07-2011 at 01:25 AM. Reason: don't need to escape the /
 
2 members found this post helpful.
Old 06-07-2011, 01:25 AM   #5
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,692

Rep: Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987
Maybe show us how you are doing the opposite and an alternate can be suggested?

Although, if in a script then David's solution is already the most efficient.
 
1 members found this post helpful.
Old 06-07-2011, 01:31 AM   #6
hattori.hanzo
Member
 
Registered: Aug 2006
Posts: 167

Original Poster
Rep: Reputation: 15
Its OK. Just figured it out with gawk. :-) I will try David's solution also and compare.

Code:
$ cat test_date.txt
02/06/2011
03/06/2011
03/06/2011
05/06/2011

$  gawk -F, '{split($1, a, "/"); print a[3] a[2] a[1]}' test_date.txt
20110602
20110603
20110603
20110605
Thanks.

Last edited by hattori.hanzo; 06-07-2011 at 01:36 AM.
 
Old 06-07-2011, 01:49 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
GNU date is extremely versatile. The -d option will accept almost any input format, even human-readable ones like "tomorrow" or "one hour ago", and you can format the output to whatever you want. See the man page for options.

So to reverse the above, just use:
Code:
date -d 20110606 +%d/%m/%Y
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] SOLVED: awk: d/m/yyyy h:mm to yyyy-mm-dd hh:mm:ss geetalicious Programming 2 05-05-2011 04:18 AM
[SOLVED] datetime format from MM-DD-YYYY HH:MM:SS to YYYY-MM-DD HH:MM:SS hattori.hanzo Programming 4 10-13-2010 08:04 PM
[SOLVED] Converting YYYYMMDD to date in Bash script knockoutned Programming 4 08-03-2010 09:07 AM
[SOLVED] extract modification date of file as yyyymmdd thelordmule Other *NIX 3 07-11-2009 03:28 AM
find YYYYMMDD and YYYYMMDD difference kpachopoulos Programming 1 09-22-2007 11:37 AM


All times are GMT -5. The time now is 05:40 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration