Skip to content

Instantly share code, notes, and snippets.

@spyesx
Last active January 15, 2019 13:47
Show Gist options
  • Save spyesx/8f30ec41f98ddbfa5d371997dd8ba6ff to your computer and use it in GitHub Desktop.
Save spyesx/8f30ec41f98ddbfa5d371997dd8ba6ff to your computer and use it in GitHub Desktop.
ISO 8601 Time with Milliseconds and Nanoseconds

For macOS, the date utility doesn't give the right information. You need to install GNU date utility.

brew install coreutils

Homebrew will install the utility as gdate instead of date, which leaves the original date untouched and available.

gdate has an --iso-8601 option available, but it doesn't give a format that strictly follows the ISO 8601 standard, as far as I can determine. I find it better to explicitly state the format with:

gdate -u +'%Y-%m-%dT%H:%M:%S.%N%z'

# 2019-01-15T09:55:09.206391000+0000

The %N is for the fractional seconds (nanoseconds). If you want milliseconds you should display only three decimal with %3N.

Note that we ask UTC time with the flag -u. Simply remove it if you need your system's time.

gdate +'%Y-%m-%dT%H:%M:%S.%N%z'

# 2019-01-15T17:56:36.325669000+0800
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment