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