How to convert between date formats on the command line

DominikHoffmann

Registered
How would I convert a string, such as
Code:
2012-01-31 18:54:55 +0000
into the format of the
Code:
date -j -f "%a %b %d %T %Z %Y" "`date`" "+%s"
i.e., Epoch time?
 

Viro

Registered
Try this:
Code:
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

Registered
That command produces the output
Code:
usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ... 
            [-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]
 

Viro

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

BjarneDM

Registered
You'll have to combine the information from man 1 date and man 3 strftime

Code:
declare myDate="2012-01-31 18:54:55 +0000"
date -j -f "%Y-%m-%d %T %z" "${myDate}" "+%s"
 
Last edited:
Top