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?
 
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.
 
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]
 
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.
 
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:
Back
Top