Unix Timestamp in Bash

In the shell, date +%s prints the current Unix timestamp in seconds. GNU date converts with date -d @ts; macOS/BSD date uses date -r ts.

Get & convert epoch time in Bash

date -u -d @1700000000      # GNU -> 2023-11-14T22:13:20.000Z
date -u -r 1700000000       # macOS/BSD
date +%s             # current epoch
Gotcha: GNU and BSD date have different flags — -d @ts works on Linux, -r ts on macOS. Scripts that must run on both should detect the platform.

Frequently asked questions

How do I get the current Unix timestamp in Bash?
date +%s prints seconds; date +%s%3N prints milliseconds on GNU date.
How do I convert a timestamp to a date in Bash?
GNU: date -u -d @ts; macOS/BSD: date -u -r ts.

Other languages

Copied