Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save dan-null/a384f31fd63667c6dde4b4738e2aad27 to your computer and use it in GitHub Desktop.

Select an option

Save dan-null/a384f31fd63667c6dde4b4738e2aad27 to your computer and use it in GitHub Desktop.
How to get an ISO 8601 timestamp with milliseconds in BASH
Gist title: "BASH: ISO 8601 Timestamp with Milliseconds"
Summary: How to get an ISO 8601 timestamp with milliseconds in BASH

For mac OS, the date utility doesn't give the right information. In that case, use homebrew to install the 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 timestamp 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.%3NZ'

Two things to not about the format string:

  1. The 3 in %3N specifies that the fractional seconds (nanoseconds) should only be displayed to three decimal places (milliseconds).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment