2

I would like to get the current date-time conveniently on the console in standard ISO 8601 format: YYYY-MM-DDTHH:MM:SSZ

For example: 2019-07-13T01:09:35Z

I know the date command displays the current date-time, but not in ISO 8601 format by default. The -I option shows the date-only portion in standard format. And -Iseconds displays 2019-07-13T01:18:10+00:00. That is close, but:

  • I would prefer the usual Z on the end for an offset of zero, rather than +00:00.
  • I would like something shorter to type, for such a basic common task.

My Question here is the same as this one, but for BSD rather than Linux (apparently).

1 Answer 1

5

Since you want "Z" (GMT) you can do it simply with a format string and the literal T and Z characters

% TZ=GMT date +"%Y-%m-%dT%H:%M:%SZ"
2019-07-13T02:04:16Z

If you don't want to type that each time, you can make it a function or a script

eg

% isotime()
{
  TZ=GMT date +"%Y-%m-%dT%H:%M:%SZ"
}

% isotime
2019-07-13T02:06:42Z
3
  • 3
    Note you need to add -u (UTC) otherwise you get the date that seems like UTC but really is in your local timezone. So the full command would be date -u +"%Y-%m-%dT%H:%M:%SZ"
    – wizzard0
    Apr 16, 2021 at 8:57
  • @wizzard0 No -u is needed. The zero-offset time zone is specified by setting the TZ variable: TZ=GMT. IMHO UTC should be used instead because the meaning of GMT is a little bit ambiguous. May 6, 2022 at 15:28
  • 1
    @pabouk-Ukrainestaystrong Yeah it should work like that, however I ran into a situation where date ignored TZ at least once, and -u worked flawlessly. Can't recall the exact environment right now tho :/
    – wizzard0
    May 7, 2022 at 17:45

You must log in to answer this question.

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