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.

[MOD.CONV // INSTRUMENT]

UNIX TIMESTAMP INPUT

Auto-detects seconds, ms, µs, ns

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.

01 Local time —
02 UTC standard —
03 ISO 8601 —
04 Relative —
[SUB.REVERSE]
SECONDS (S) —
MILLISECONDS (MS) —

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.

[SEC.02 // TWO DIALECTS]

GNU date -d vs BSD date -r

SAME WORD, DIFFERENT FLAGS

Both dialects can print the current epoch seconds with date +%s. They diverge as soon as you ask for another moment:

TaskGNU coreutils (Linux)BSD / macOS
Now, in secondsdate +%sdate +%s
Epoch number → datedate -d @1700000000date -r 1700000000
Date text → epoch numberdate -d "2023-11-14" +%sdate -j -f "%Y-%m-%d %H:%M:%S" "2023-11-14 00:00:00" "+%s"
Force UTC on outputdate -u -d @1700000000TZ=UTC date -r 1700000000
ISO 8601 outputdate -Insdate "+%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.

[SEC.03 // THE %N TRAP]

Nanoseconds that are actually a letter

CHECK OS AND VERSION

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 wantGNUPortable
Whole secondsdate +%sdate +%s (both dialects)
Millisecondsdate +%s%3Nprintf "%s000\n" "$(date +%s)" — second precision only
Nanosecondsdate +%s%NWith Python 3.7+: python3 -c "import time;print(time.time_ns())"
Microsecond-scale readingdate +%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 …N string 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 +%s is identical on both systems, and the current Unix time page shows the four-unit reading you would otherwise reconstruct by hand.
[SEC.04 // WITHOUT SPAWNING DATE]

Bash built-in clock variables and printf

BASH 4.2+ / 5.0+
  • $EPOCHSECONDS (Bash 5+) is the current integer Unix second; $EPOCHREALTIME is 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: -1 means now, and an integer argument is read as an epoch second count. To force UTC, prefix the command with TZ=UTC.
  • SECONDS counts 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 @1700000000 scopes the zone to one command only, which is safer than exporting TZ for the whole script. The Z suffix and offset rules are on the timestamp to ISO 8601 page.
[SEC.05 // PORTABLE SCRIPTING]

Parsing, rounding and cross-language checks

ONE NUMBER, MANY TOOLS
  • @ is GNU-only: date -d @1700000000 is 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 the TZ of the process, so the same text produces different numbers on different machines. Pin TZ=UTC or write an explicit offset.
  • Integer vs float: shell arithmetic is integer-only, so $(date +%s) * 1000 is exact but microtime-style fractional seconds need awk or python3. 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.
[DOC.FAQ // INDEX]

ANSWERS

06 ITEMS
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.

[SEC.MATRIX // RELATED]

Related tools

3 PAGES

Sources and specifications