View Full Version : How to convert between date formats on the command line
DominikHoffmann
February 1st, 2012, 08:29 PM
How would I convert a string, such as
2012-01-31 18:54:55 +0000
into the format of the
date -j -f "%a %b %d %T %Z %Y" "`date`" "+%s"
i.e., Epoch time?
Viro
February 3rd, 2012, 10:48 AM
Try this:
date --date='2012-01-31 18:54:55 +0000' +'%a %b %d %T %Z %Y'
That code produces "Tue Jan 31 18:54:55 GMT 2012" which is presumably what you're after.
DominikHoffmann
February 3rd, 2012, 11:09 AM
That command produces the output
usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ...
[-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]
Viro
February 3rd, 2012, 03:42 PM
Hmm... it looks like the shell utilities that are shipped with OS X aren't standard. Sorry I can't help more as I don't have my Mac with me.
DominikHoffmann
February 3rd, 2012, 03:44 PM
Try this:
[CODE]
That code produces "Tue Jan 31 18:54:55 GMT 2012" which is presumably what you're after.
I'm looking for the time in seconds since Jan. 1, 1970.
BjarneDM
February 3rd, 2012, 05:03 PM
You'll have to combine the information from man 1 date and man 3 strftime
declare myDate="2012-01-31 18:54:55 +0000"
date -j -f "%Y-%m-%d %T %z" "${myDate}" "+%s"