Skip to content

Instantly share code, notes, and snippets.

@sloanlance
Last active February 9, 2023 19:33
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sloanlance/7606480599d852ba1ce22451758b5985 to your computer and use it in GitHub Desktop.
Save sloanlance/7606480599d852ba1ce22451758b5985 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).
@meowsbits
Copy link

meowsbits commented Apr 18, 2020

typo: %Y-%m-%dT%H:%M:%S.%3NZ should be %Y-%m-%dT%H:%M:%S.%3N%Z (the Z needs to be %Z).

.. and I think for ISO8601 should be Z%Z .. ?

@jsejcksn
Copy link

Thanks for this gist. It's a shame that the macOS version doesn't support finer granularity than seconds.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment