date command on Linux & macOS. Working examples for every common operation — get current time, convert epoch to date, convert date to epoch, format display, parse, and handle timezones.
`date +%s` is universal across Linux, macOS, and other Unix systems.
date +%s
# → 1735689600
date +%s%3N
# → 1735689600123
# or:
date +%s.%3N
# macOS's BSD date doesn't support %N.
# Use gdate (from coreutils) if available, or:
perl -MTime::HiRes -E 'printf "%d\n", Time::HiRes::time() * 1000'
date +%s%N
# → 1735689600123456789
Linux GNU and macOS BSD have different syntax for this.
date -d @1735689600
# → Wed Jan 1 00:00:00 UTC 2025
# explicit UTC
date -u -d @1735689600
# custom format
date -d @1735689600 +"%Y-%m-%dT%H:%M:%SZ"
# → 2025-01-01T00:00:00Z
date -r 1735689600
# → Tue Dec 31 19:00:00 EST 2024
# explicit UTC
date -u -r 1735689600
# custom format
date -r 1735689600 "+%Y-%m-%dT%H:%M:%SZ"
Again, different syntax per platform.
date -d "2025-01-01 00:00:00 UTC" +%s
# → 1735689600
# from a variable
input="2025-06-15 12:00:00"
date -d "$input" +%s
date -j -u -f "%Y-%m-%d %H:%M:%S" "2025-01-01 00:00:00" +%s
# → 1735689600
# the format string (-f) must match the input exactly
perl -MDate::Parse -E 'say str2time("2025-01-01")'
# → 1735689600
Set TZ environment variable to convert into a different timezone.
TZ="America/New_York" date -d @1735689600
# → Tue Dec 31 19:00:00 EST 2024
# macOS
TZ="America/New_York" date -r 1735689600
# IANA timezone database is usually at /usr/share/zoneinfo
ls /usr/share/zoneinfo
# or:
timedatectl list-timezones # systemd systems
Common command-line patterns developers actually need.
epoch_then=1700000000
seconds_ago=$(( $(date +%s) - epoch_then ))
echo "$seconds_ago seconds ago"
date -d "+1 hour" +%s # GNU
date -v +1H +%s # BSD/macOS
# log file has epoch seconds in column 1, replace with human-readable:
awk '{ "date -d @"$1" +%Y-%m-%dT%H:%M:%SZ" | getline d; $1=d; print }' logfile.txt
echo 2147483647 | xargs -I{} date -d "@{}" -u
# → Tue Jan 19 03:14:07 UTC 2038
Bash date pitfalls and platform differences.
# These tools have totally incompatible flags:
# Linux GNU date: -d for input parsing, @ for epoch
# macOS BSD date: -r for epoch input, -j -f for parsing
# For portable scripts, install GNU coreutils on macOS (brew install coreutils)
# and use 'gdate' instead of 'date'.
# date -d @1735689600 works on Linux
# date -d @1735689600 FAILS on macOS
# Use the platform-specific syntax above instead.
# date's default format respects LC_TIME.
# For predictable output in scripts, explicitly specify a format:
date -d @1735689600 +%s # always digits
date -d @1735689600 # locale-dependent text
Need to quickly check what a specific timestamp converts to? Use the main converter — paste any value and get every format back.
Working with many timestamps at once? Try the batch converter — paste a list, get a CSV or JSON file back.