Bash date and Unix timestamps
There is no single date command: GNU coreutils and BSD/macOS ship two different dialects that disagree on both directions. Convert a number below, then read the portable forms, the %N trap that silently prints a letter, and the clock variables Bash gives you for free.
UNIX TIMESTAMP INPUT
Date output is floored to milliseconds. Choose a unit explicitly for older dates or values outside auto-detection.
Typical today: 10 (s), 13 (ms), 16 (µs), 19 (ns) digits. A unit heuristic, not a definition. Nnow Ccopy /focus
Local means your device timezone. During a repeated daylight-saving hour, the earlier occurrence is used; use UTC to choose an unambiguous instant.
Browser-side reference calculator. It does not execute Java, Go, PHP or shell commands; use the examples below in your own runtime.
GNU date -d vs BSD date -r
Both dialects can print the current epoch seconds with date +%s. They diverge as soon as you ask for another moment:
| Task | GNU coreutils (Linux) | BSD / macOS |
|---|---|---|
| Now, in seconds | date +%s | date +%s |
| Epoch number → date | date -d @1700000000 | date -r 1700000000 |
| Date text → epoch number | date -d "2023-11-14" +%s | date -j -f "%Y-%m-%d %H:%M:%S" "2023-11-14 00:00:00" "+%s" |
| Force UTC on output | date -u -d @1700000000 | TZ=UTC date -r 1700000000 |
| ISO 8601 output | date -Ins | date "+%Y-%m-%dT%H:%M:%S%z" |
The -r flag is the nastiest false friend: on BSD it takes epoch seconds; on GNU it takes a file and prints that file’s modification time (date -r access.log). So date -r 1700000000 works on macOS and fails on Linux with "cannot stat". If a script must run on both, branch on date --version or use the Bash built-ins below instead.
Nanoseconds that are actually a letter
GNU date supports %N for nanoseconds, but support is version-dependent across BSD-derived systems. On macOS versions without it, date +%s%N can print a literal N rather than digits; some newer BSD date implementations support %N. Check the installed manual and actual output, or explicitly require GNU coreutils.
| You want | GNU | Portable |
|---|---|---|
| Whole seconds | date +%s | date +%s (both dialects) |
| Milliseconds | date +%s%3N | printf "%s000\n" "$(date +%s)" — second precision only |
| Nanoseconds | date +%s%N | With Python 3.7+: python3 -c "import time;print(time.time_ns())" |
| Microsecond-scale reading | date +%s%6N (integer µs) | $EPOCHREALTIME (Bash 5+): fractional seconds, not integer µs |
- Never validate with length alone. A 19-digit nanosecond reading and an 11-character
…Nstring both look like numbers at a glance; assert digits with[[:digit:]]before arithmetic. - Seconds are the portable unit. If a log pipeline only needs second precision,
date +%sis identical on both systems, and the current Unix time page shows the four-unit reading you would otherwise reconstruct by hand.
Bash built-in clock variables and printf
$EPOCHSECONDS(Bash 5+) is the current integer Unix second;$EPOCHREALTIMEis the same with a microsecond fraction and a locale-sensitive decimal separator. Both are variables, so no subprocess and no dialect check.TZ=UTC printf "%(%Y-%m-%dT%H:%M:%SZ)T" -1(Bash 4.2+) formats a time using strftime inside the shell:-1means now, and an integer argument is read as an epoch second count. To force UTC, prefix the command withTZ=UTC.SECONDScounts seconds since the shell started, not since 1970 — despite the name it is an elapsed timer, not a timestamp. Mixing it into an epoch calculation is a classic copy-paste bug.TZ=UTC date -d @1700000000scopes the zone to one command only, which is safer than exportingTZfor the whole script. TheZsuffix and offset rules are on the timestamp to ISO 8601 page.
Parsing, rounding and cross-language checks
@is GNU-only:date -d @1700000000is a coreutils convention. BSD needs-r 1700000000. There is no shared flag between them.- Zone-less input is local: GNU
date -d "2023-11-14"reads midnight in theTZof the process, so the same text produces different numbers on different machines. PinTZ=UTCor write an explicit offset. - Integer vs float: shell arithmetic is integer-only, so
$(date +%s) * 1000is exact butmicrotime-style fractional seconds needawkorpython3. The milliseconds to seconds page covers the floor-vs-round rule. - Cross-check the number: the timestamp to date converter auto-detects units within limited numeric ranges; select an explicit unit when the source unit is known, and the Go, Java and PHP pages show the same unit split in application code.
ANSWERS
01 How do I get the current Unix timestamp in Bash?
Use date +%s on both GNU/Linux and macOS. On Bash 5+ you can read $EPOCHSECONDS with no subprocess, or $EPOCHREALTIME for seconds with a microsecond fraction, not an integer microsecond count. For milliseconds on GNU, date +%s%3N; on macOS, append three zeros to date +%s — that keeps second precision only.
02 How do I convert an epoch number to a date in a shell script?
GNU: date -d @1700000000 (add -u for UTC). BSD/macOS: date -r 1700000000. Neither flag works on the other dialect, so detect with date --version >/dev/null 2>&1 or use Bash printf "%(... )T" "$ts".
03 Why does date +%s%N print a letter N on macOS?
The installed date implementation may not support %N. GNU date supports it; BSD-derived implementations differ by OS and version, and unsupported macOS versions may emit a literal N. Check the local manual and validate digit-only output before arithmetic.
04 What is the difference between date -r on Linux and macOS?
On BSD/macOS, -r takes epoch seconds and prints that moment. On GNU, -r takes a file path and prints the file’s modification time. The same command with a numeric argument therefore works on macOS and fails on Linux.
05 How do I convert a date string to epoch seconds portably?
There is no single flag. GNU uses date -d "2023-11-14 00:00:00 UTC" +%s; BSD uses date -j -f "%Y-%m-%d %H:%M:%S" "2023-11-14 00:00:00" "+%s" (the -j means do not set the clock). Pin TZ=UTC so both interpret the text identically, or delegate to Python.
06 Is Bash $SECONDS a Unix timestamp?
No. SECONDS counts elapsed seconds since that shell started. $EPOCHSECONDS (Bash 5+) is the real Unix second count. The similar names are a common source of timestamps that look valid but sit near 1970.