Qiang Li
Qiang Li

Reputation: 10855

mac using date to show seconds has "illegal time format" error

I tried the following on mac terminal, and found it has some problem:

date –j –f '%d-%b-%Y' "22-Aug-2013" "+%s"
date: illegal time format
usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ... 
            [-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]

Could anybody help me to parse this string 22-Aug-2013 and get the epoch seconds?

Upvotes: 8

Views: 23649

Answers (2)

Nicholas Riley
Nicholas Riley

Reputation: 44321

You're using the wrong character before your option characters j and f — you’re using an en dash (–, U+2013) rather than a hyphen (-, U+002D). Unix tools don't tend to be terribly Unicode savvy when parsing command-line arguments :-)

You may find TextWrangler/BBEdit’s Character Inspector palette useful, or if you're more of an Emacs person, M-x describe-char.

Upvotes: 10

James Allman
James Allman

Reputation: 41158

I had no issue in OS X 10.8.4:

$ date -j -f '%d-%b-%Y' "22-Aug-2013" "+%s"
1377223888

$ date -j -f '%s' 1377223888
Thu Aug 22 21:11:28 CDT 2013

Note that it is taking the current time and including it with the specified date. It would be more accurate to explicitly set the time:

$ date -j -f '%d-%b-%Y %T' "22-Aug-2013 00:00:00" "+%s"
1377147600

$ date -j -f '%s' 1377147600
Thu Aug 22 00:00:00 CDT 2013

Upvotes: 4

Related Questions