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
date -Iseconds
seems to "just work" on all platformsdate -I
is working at UBUNTU