`date` command on OS X doesn't have ISO 8601 `-I` option?

Aseem Kishore picture Aseem Kishore · Aug 27, 2011 · Viewed 87.4k times · Source

In a Bash script, I want to print the current datetime in ISO 8601 format (preferably UTC), and it seems that this should be as simple as date -I:

http://ss64.com/bash/date.html

But this doesn't seem to work on my Mac:

$ date -I
date: illegal option -- I
usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ... 
            [-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]

And indeed, man date doesn't list this option.

Anyone know why this is, or any other (easy) way for me to print the date in ISO 8601 format? Thanks!

Answer

amit_g picture amit_g · Aug 27, 2011

You could use

date "+%Y-%m-%d"

Or for a fully ISO-8601 compliant date, use one of the following formats:

date -u +"%Y-%m-%dT%H:%M:%SZ"

Output:

2011-08-27T23:22:37Z

or

date +%Y-%m-%dT%H:%M:%S%z

Output:

2011-08-27T15:22:37-0800