240

This answer and comments mention --rfc-3339 and a "hidden" --iso-8601 option that I have used for a long time and now seems to be undocumented.

When did that option documentation get removed from the --help text?

Will the option go away anytime soon?

4
  • 3
    To get this feature on OSX I finally got the GNU tools: $ brew install coreutils ; gdate --iso-8601 -d "yesterday 12:00" Commented Jul 5, 2018 at 18:51
  • 23
    @AnneTheAgile for macOS, you can manually format the date like so: "date +%Y-%m-%dT%H:%M:%S%z"
    – Bart Doe
    Commented Apr 23, 2019 at 12:47
  • 3
    I feel like the top answers are pretty complicated considering that date -Iseconds seems to "just work" on all platforms Commented Oct 4, 2022 at 17:46
  • in 2023 date -I is working at UBUNTU Commented Apr 18, 2023 at 21:25

5 Answers 5

166

The option was introduced in the coreutils date (which is probably what you have) in 1999 (Apr. 8).

The documentation was removed in 2005 without much explanation in the commit.

In 2011, the help for --iso-8601 was reintroduced with the following explanation:

We deprecated and undocumented the --iso-8601 (-I) option mostly
because date could not parse that particular format.  Now that
it can, it's time to restore the documentation.
* src/date.c (usage): Document it.
* doc/coreutils.texi (Options for date): Reinstate documentation.
Reported by Hubert Depesz Lubaczewski in http://bugs.gnu.org/7444.

It looks like the help was taken out in version 5.90 and put back in, in version 8.15 (it is not in my 8.13) and the comment above suggests that it is now back to stay and not likely to be disappearing any time soon.

In version 8.31 (as provided by Solus July 2020) the man page has these two options:

   -I[FMT], --iso-8601[=FMT]
          output date/time in ISO 8601 format.  FMT='date' for date only (the default), 'hours', 'minutes', 'sec‐
          onds', or 'ns' for date and time to the indicated precision.  Example: 2006-08-14T02:34:56-06:00

   --rfc-3339=FMT
          output date/time in RFC 3339 format.  FMT='date', 'seconds', or 'ns' for date and time to the indicated
          precision.  Example: 2006-08-14 02:34:56-06:00
0
156

For a platform independent, (almost) fully compliant, ISO 8601 date, use this:

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

This will result in a time such as: 2021-01-16T23:09:44-0500

This should work on macOS (BSD) and Linux.

Technically, for full compliance, it should be:

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

Note the offset (time zone) field contains a colon ':' between the '%' and the 'z'.  This will result in a fully complaint ISO 8601 time such as: 2021-01-16T23:09:44-05:00

But this doesn't work on macOS (as of this writing). This should work on Linux (i.e., with GNU date).

For UTC time (zero offset, historically known as "Zulu time") you can use (note the -u, to get UTC time, and note that the 'Z' is not preceded by a '%' (or a colon) – it is a literal 'Z'):

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

This will result in a time such as: 2021-01-17T04:16:14Z

5
  • 4
    Mac OSX seems happy now w/zsh & bash: % date +"%Y-%m-%dT%H:%M:%S%:z" yields 2022-03-22T11:05:42:z on Darwin 21.3.0 Darwin Kernel Version 21.3.0.
    – jgreve
    Commented Mar 22, 2022 at 17:11
  • 6
    @jgreve no, you just showed that Mac's date does not understand the %:z format control. Check your output thoroughly, it wrote literal :z instead of your time zone with the : separator like for example date +%:z -> +02:00 --- Also shell normally has nothing to do with this because date is normally an external executable, not the shell's built-in. Commented May 6, 2022 at 15:12
  • 1
    argh. :-/ I stand corrected.
    – jgreve
    Commented May 6, 2022 at 16:36
  • 2
    MacOS 12.6 supports date -Iseconds, can't figure out when this was changed though!
    – Fizzadar
    Commented Oct 9, 2022 at 18:05
  • The first example also is not fully portable, as %z is not defined (only %Z is defined) in the POSIX standard for date(1) (see pubs.opengroup.org/onlinepubs/9699919799/utilities/date.html) Commented Jul 16, 2023 at 17:21
63

The --help got an update recently actually, so the option definitely isn't going away:

-I[FMT], --iso-8601[=FMT]  output date/time in ISO 8601 format.
                             FMT='date' for date only (the default),
                             'hours', 'minutes', 'seconds', or 'ns'
                             for date and time to the indicated precision.
                             Example: 2006-08-14T02:34:56-06:00

     -R, --rfc-2822        output date and time in RFC 2822 format.
                             Example: Mon, 14 Aug 2006 02:34:56 -0600

         --rfc-3339=FMT    output date/time in RFC 3339 format.
                             FMT='date', 'seconds', or 'ns'
                             for date and time to the indicated precision.
                             Example: 2006-08-14 02:34:56-06:00

Note since coreutils-8.27 --rfc-2822 is deprecated in favor of the more general --rfc-email

     -R, --rfc-email       output date and time in RFC 5322 format.
                             Example: Mon, 14 Aug 2006 02:34:56 -0600
2
  • 7
    It would be nice if there was a -I style without the timezone. Would be more useful for logging, etc, where the timezone doesn't matter
    – naught101
    Commented Jan 17, 2019 at 5:56
  • 2
    @naught101 you can get rid of the timezone offset by specifying the component parts: date +"%Y-%m-%dT%H:%M:%S.
    – enharmonic
    Commented Jan 23, 2023 at 22:13
57

I'm running Linux Mint and the option is available:

$ lsb_release -a
No LSB modules are available.
Distributor ID: LinuxMint
Description:    Linux Mint 17.3 Rosa
Release:    17.3
Codename:   rosa

The execution of the command:

$ date --iso-8601=seconds
2016-12-14T09:53:25-0400
6
  • 27
    or just date -Iseconds
    – pstanton
    Commented Nov 15, 2019 at 1:29
  • 14
    but it doesn't appends Z to the end so this is not fully complete ISO timestamp Commented Jan 7, 2020 at 10:34
  • 6
    date -In (same as date -Ins) or date -Is same as (date -Iseconds) etc.
    – Artfaith
    Commented Jun 13, 2021 at 19:08
  • 7
    "but it doesn't appends Z to the end so this is not fully complete ISO timestamp" Z is the UTC timezone. +00:00 is the same as Z. +00:00Z is not correct. Commented Jul 20, 2021 at 17:08
  • 3
    For a filename compatible format, I use $(date -u +"%Y-%m-%dT%H-%M-%SZ")
    – SandRock
    Commented May 20, 2022 at 9:48
3

I think this is perfect, maybe you will also like it:

date -u --iso-8601=ns | sed s/+00:00/Z/ | sed s/,/./

Result example: 2022-12-10T20:44:36.753578818Z

You must log in to answer this question.

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