5

I'm sure this used to be valid:

$ date -d"2013-10-12T09:23:28+0100" +%h
usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ...
        [-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]

But it seems not to be in Mavericks. Indeed, the example for parsing dates in man date also doesn't seem to work:

$ date -j -f "%a %b %d %T %Z %Y" "`date`" "+%s"
Failed conversion of ``Thu 31 Oct 2013 21:05:29 GMT'' using format ``%a %b %d %T %Z %Y''
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]

Any ideas on a workaround?

1
  • 2
    What output to you desire from your date command?
    – bmike
    Oct 31, 2013 at 21:22

2 Answers 2

6

Here are two date commands that work on Mavericks to convert/extract time from date strings:

mac:~ me$ date -j -f "%Y-%m-%d %T" "2013-10-12 09:23:28" +%s
1381584208
mac:~ me$ date -j -f "%Y/%m/%d %T" "2013/10/22 10:45:00" +%h
Oct

It's not entirely clear what you seek, but hopefully these workable examples bridge the gap for your needs.

2
  • Thanks. I was just looking to parse dates. These work, which is great, although I actually installed coreutils via homebrew and switched to gdate to resolve it.
    – rich
    Oct 31, 2013 at 22:12
  • If you don't mind the install, gdate has some much nicer features so you can pick and choose your particular poison for each task.
    – bmike
    Oct 31, 2013 at 22:14
1

-d has set the kernel's value for daylight saving time in OS X's date at least since 10.6. The first command worked for me with GNU date (where -d specifies a date string):

$ gdate -d"2013-10-12T09:23:28+0100" +%h
Oct

The second command worked with the date that comes with 10.9 when I used the en_US locale but not when I used the en_GB locale:

$ date -j -f "%a %b %d %T %Z %Y" "`date`" "+%s"
1383305706
$ date -j -f "%a %b %d %T %Z %Y" "`LC_TIME=en_GB date`" "+%s"
Failed conversion of ``Fri  1 Nov 2013 14:09:17 EET'' using format ``%a %b %d %T %Z %Y''
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]

In OS X's date, -j disables attempting to set the date and -f specifies a format for parsing a date.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .