Unix timestamps in Bash with date
date +%s works everywhere. Converting in either direction does not: GNU date on Linux and BSD date on macOS use different options for the same job.
Current timestamp
date +%s # → e.g. 1763164800
date +%s%3N # → e.g. 1763164800123 milliseconds (GNU date)%s is supported by GNU, BSD and BusyBox date. %N (nanoseconds) is GNU only; on macOS date +%s%3N prints a literal 3N. Bash 5 also has the variables $EPOCHSECONDS and $EPOCHREALTIME, which need no external command.
Timestamp to date (GNU date, Linux)
Every example uses 1700000000, which is 14 November 2023 at 22:13:20 UTC, so you can compare the output across languages or check it in the timestamp converter.
date -u -d @1700000000 +'%Y-%m-%dT%H:%M:%SZ' # → 2023-11-14T22:13:20Z
date -u -d @1700000000 # → Tue Nov 14 22:13:20 UTC 2023TZ=America/New_York date -d @1700000000 +'%F %T %Z' # 2023-11-14 17:13:20 ESTThe @ marks the argument as a timestamp. Without -u the output is in the zone given by the TZ variable, or the system zone if it is not set.
Date to timestamp (GNU date, Linux)
date -u -d '2023-11-14 22:13:20' +%s # → 1700000000
date -d '2023-11-14T22:13:20Z' +%s # → 1700000000
date -d '2023-11-14 17:13:20 -0500' +%s # → 1700000000A date string with no zone is read as local time unless you add -u.
macOS and BSD date
date -r 1700000000 # epoch to local date
date -u -r 1700000000 +'%Y-%m-%dT%H:%M:%SZ' # 2023-11-14T22:13:20Z
date -j -u -f '%Y-%m-%d %H:%M:%S' '2023-11-14 22:13:20' +%s # 1700000000BSD date has no -d @. -r takes the seconds, and -j -f parses a string with an explicit format without trying to set the clock. On GNU date, -r means something else: the modification time of a file. If you install coreutils with Homebrew, the GNU version is available as gdate.
Arithmetic
echo $(( 1700000000 + 7 * 86400 )) # → 1700604800 one week later
date -u -d @$(( 1700000000 + 7 * 86400 )) +%F # → 2023-11-21
echo $(( ($(date +%s) - 1700000000) / 86400 )) # → e.g. 731 whole days since then
echo $(( 1700000000123 / 1000 )) # → 1700000000 milliseconds to secondsShell arithmetic is 64-bit integer, so milliseconds fit, and division truncates. Inside Bash, printf '%(%F %T)T\n' 1700000000 formats a timestamp in local time without starting a process (Bash 4.2 and later).
Questions
How do I convert epoch to a date on the Linux command line?
date -d @1700000000 prints it in local time; add -u for UTC and a +FORMAT such as +%F\ %T to choose the layout.
Why does date -d not work on my Mac?
macOS ships BSD date, where -d is not the option for a date string. Use date -r 1700000000 to convert a timestamp, or install GNU coreutils and use gdate.
How do I get milliseconds in Bash?
With GNU date, date +%s%3N. In Bash 5 without date, use ${EPOCHREALTIME/[.,]/} for microseconds and drop the last three digits. macOS date has no sub-second format.