Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 800
Aseem Kishore Avatar asked Aug 27 '11 18:08

Aseem Kishore


People also ask

What time zone is ISO 8601?

Time zones in ISO 8601 are represented as local time (with the location unspecified), as UTC, or as an offset from UTC.

Is UTC same as ISO 8601?

Current time: 23:36:04 UTC. UTC is replaced with Z that is the zero UTC offset. UTC time in ISO-8601 is 23:36:04Z.

How do I print the date in an ISO file?

In Python ISO 8601 date is represented in YYYY-MM-DDTHH:MM:SS. mmmmmm format. For example, May 18, 2022, is represented as 2022-05-18T11:40:22.519222.


8 Answers

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
like image 95
amit_g Avatar answered Sep 30 '22 17:09

amit_g


In GNU date date -I is the same as date +%F, and -Iseconds and -Iminutes also include time with UTC offset.

$ date +%F # -I or +%Y-%m-%d
2013-05-03
$ date +%FT%T%z # -Iseconds or +%Y-%m-%dT%H:%M:%S%z
2013-05-03T15:59:24+0300
$ date +%FT%H:%M # -Iminutes or +%Y-%m-%dT%H:%M%z
2013-05-03T15:59+0300

-u is like TZ=UTC. +00:00 can be replaced with Z.

$ date -u +%FT%TZ
2013-05-03T12:59:24Z

These are also valid ISO 8601 date or time formats:

20130503T15 (%Y%m%dT%M)
2013-05 (%Y%m)
2013-W18 (%Y-W%V)
2013-W18-5 (%Y-W%V-%u)
2013W185 (%YW%V%u)
2013-123 (%Y-%j, ordinal date)
2013 (%Y)
1559 (%H%M)
15 (%H)
15:59:24+03 (UTC offset doesn't have to include minutes)

These are not:

2013-05-03 15:59 (T is required in the extended format)
201305 (it could be confused with the YYMMDD format)
20130503T15:59 (basic and exteded formats can't be mixed)
like image 26
Lri Avatar answered Sep 30 '22 17:09

Lri


A short alternative that works on both GNU and BSD date is:

date -u +%FT%T%z
like image 28
SomeGuy Avatar answered Sep 30 '22 16:09

SomeGuy


The coreutils package provides GNU versions of tools. To install:

$ brew install coreutils

You can see what's provided:

$ brew list coreutils

Notice it comes with date:

$ brew list coreutils | grep date

This is the standard GNU date command so it'll take the -I switch:

$ gdate -I
2016-08-09
like image 25
slm Avatar answered Sep 30 '22 17:09

slm


Just use normal date formatting options:

date '+%Y-%m-%d'

Edit: to include time and UTC, these are equivalent:

date -u -Iseconds

date -u '+%Y-%m-%dT%k:%M:%S%z'
like image 32
Tom Zych Avatar answered Sep 30 '22 18:09

Tom Zych


Taking the other answers one step further, you could add a function to your ~/.bashrc or ~/.zshrc to add the date -I flag:

date() {
  if [ "$1" = "-I" ]; then
    command date "+%Y-%m-%dT%H:%M:%S%z"
  else
  command date "$@"
  fi
}
like image 36
ruby_slinger Avatar answered Sep 30 '22 16:09

ruby_slinger


It's not a feature of Bash, it's a feature of the date binary. On Linux you would typically have the GNU coreutils version of date, whereas on OSX you would have the BSD legacy utilities. The GNU version can certainly be installed as an optional package, or you can roll your own replacement - I believe it should be a simple one-liner e.g. in Perl.

like image 40
tripleee Avatar answered Sep 30 '22 16:09

tripleee


There's a precompiled coreutils package for Mac OS X available at:

http://rudix.org/packages-abc.html#coreutils.

like image 30
tonyk Avatar answered Sep 30 '22 17:09

tonyk