A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://stackoverflow.com/questions/7216358/date-command-on-os-x-doesnt-have-iso-8601-i-option below:

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

Asked 13 years, 11 months ago

Viewed 117k times

Closed 6 months ago.

The community reviewed whether to reopen this question 6 months ago and left it closed:

Original close reason(s) were not resolved

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?

desertnaut

60.7k3232 gold badges155155 silver badges183183 bronze badges

asked Aug 27, 2011 at 18:05

Aseem KishoreAseem Kishore

10.9k1111 gold badges5454 silver badges5959 bronze badges

8

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
Matt Kantor

2,78622 gold badges2525 silver badges4545 bronze badges

answered Aug 27, 2011 at 18:12

amit_gamit_g

31.3k88 gold badges6666 silver badges123123 bronze badges

9

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)

answered May 3, 2013 at 14:11

LriLri

27.8k88 gold badges8787 silver badges8383 bronze badges

1

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

date -u +%FT%T%z

answered Dec 7, 2015 at 16:27

SomeGuySomeGuy

28133 silver badges22 bronze badges

1

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
devoutsalsa

1,20011 gold badge1313 silver badges2525 bronze badges

answered Aug 9, 2016 at 16:29

slmslm

16.5k1313 gold badges118118 silver badges131131 bronze badges

1

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'

answered Aug 27, 2011 at 18:11

Tom ZychTom Zych

13.6k99 gold badges3838 silver badges5555 bronze badges

1

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
}

answered Oct 11, 2018 at 12:55

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.

answered Aug 27, 2011 at 18:14

tripleeetripleee

191k3636 gold badges314314 silver badges363363 bronze badges

1

I regularly use 'date -I' in Linux when saving files. ex: touch x.date -I. While the equivalent in MacOS is 'date +%F', it is a bit awkward to type every time I save a file. So, I set an alias "alias dt='date +%F'" then touch x.dt gives me the date.

answered Aug 16, 2020 at 11:32

So many years later, but in macOS 12 (Monterey) man date does include the -I option, but it generates the same error, unless you dig further and find

 date -u -I seconds
2023-08-04T03:59:20+00:00

I note that it doesn't append "Z" to the end, but the -u option did make it output in UTC.

answered Aug 4, 2023 at 4:03

1

for the one in 2025 on Mac OS 15

date -u -Iseconds
2025-03-04T09:10:23+00:00

no space in -I and seconds

usage: date [-jnRu] [-I[date|hours|minutes|seconds]] [-f input_fmt]
            [-r filename|seconds] [-v[+|-]val[y|m|w|d|H|M|S]]
            [[[[mm]dd]HH]MM[[cc]yy][.SS] | new_date] [+output_fmt]

you can use sed later on like I did to remove +00:00

date -u -Iseconds | sed 's/+.*//'
2025-03-04T08:57:29

this is output from linux for iso-8601

date -u --iso-8601=seconds
2025-03-04T09:13:16+00:00
gladiatorgladiator

1,37388 silver badges2323 bronze badges

SS64 has a separate section for Mac commands which maybe didn't exist when this question was first asked https://ss64.com/mac/date.html

From the examples:
Print the date as an ISO 8601 compliant date/time string:

$ date -I
$ date -Iminutes
$ date -Iseconds
SS64SS64

36355 gold badges1616 silver badges2929 bronze badges

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4