Unix timestamps in Bash
Everything Bash needs for Unix timestamps: the current epoch time in seconds and milliseconds, timestamp-to-date and date-to-timestamp conversion, and ISO 8601 parsing. The catch on the command line is that GNU date (Linux) and BSD date (macOS) disagree on almost every flag, so both variants are shown below. There's a live converter at the bottom for checking any value the snippets produce.
Get the current Unix timestamp
date +%s # 1700000000, every system
date +%s%3N # milliseconds, GNU date only (Linux)On macOS, %N prints a literal N. For milliseconds there, install coreutils and use gdate +%s%3N.
Convert a timestamp to a date
date -u -d @1700000000 # Linux (GNU)
date -u -r 1700000000 # macOS / BSD
# Tue Nov 14 22:13:20 UTC 2023Convert a date to a timestamp
date -d "2023-11-14T22:13:20Z" +%s # Linux (GNU)
date -j -u -f "%Y-%m-%dT%H:%M:%SZ" \
"2023-11-14T22:13:20Z" +%s # macOS / BSDPrint the current time as ISO 8601
date -u +"%Y-%m-%dT%H:%M:%SZ" # 2023-11-14T22:13:20Z
date -u -Iseconds # GNU: 2023-11-14T22:13:20+00:00Check a value
Detected: Unix timestamp (seconds)
Your timezone
UTC · GMT+00:00 · UTC
Tue, Nov 14, 2023, 10:13:20 PM
Common formats
UTC
Tue, Nov 14, 2023, 10:13:20 PM
ISO 8601
2023-11-14T22:13:20.000Z
Unix (seconds)
1700000000
Unix (millis)
1700000000000
Relative
…
World clock
Los Angeles PST
Tue, Nov 14, 2023, 02:13:20 PM
GMT-08:00
Chicago CST
Tue, Nov 14, 2023, 04:13:20 PM
GMT-06:00
New York EST
Tue, Nov 14, 2023, 05:13:20 PM
GMT-05:00
London GMT
Tue, Nov 14, 2023, 10:13:20 PM
GMT+00:00
Paris CET
Tue, Nov 14, 2023, 11:13:20 PM
GMT+01:00
Kolkata IST
Wed, Nov 15, 2023, 03:43:20 AM
GMT+05:30
Shanghai CST
Wed, Nov 15, 2023, 06:13:20 AM
GMT+08:00
Tokyo JST
Wed, Nov 15, 2023, 07:13:20 AM
GMT+09:00
Sydney AEDT
Wed, Nov 15, 2023, 09:13:20 AM
GMT+11:00
Common questions
- Why does my date command work on Linux but not macOS?
- Linux ships GNU date, macOS ships BSD date, and the flags differ: -d vs -r for reading a timestamp, and BSD needs -j -f with an explicit format to parse strings. brew install coreutils gets you GNU behaviour as gdate.
- How do I timestamp log lines or filenames?
- For filenames use a sortable form like $(date -u +%Y%m%dT%H%M%SZ). For durations in scripts, capture start=$(date +%s) and subtract; whole seconds is usually plenty.
- What timezone does date use?
- The TZ environment variable, falling back to the system zone. Prefix any single command to override: TZ=UTC date, or TZ=Asia/Tokyo date -d @1700000000.