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

后端 未结 9 1510

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://ss

9条回答
  •  无人及你
    2021-01-29 20:08

    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)
    

提交回复
热议问题