Skip to main content

Fixed: date: illegal option -- d on macOS

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

date -d @<seconds> turns a Unix timestamp into a date on Linux, and most answers online give that form. On a Mac, the same command stops with date: illegal option -- d and prints a usage message. macOS's date reads a timestamp with -r instead.

What I Was Doing​

I was writing How to Convert a Unix Timestamp to a Date in the Terminal and needed the date for the timestamp 1800000000. On my Mac, in zsh, I ran the Linux form of the command:

Terminal
date -u -d @1800000000

What I Expected​

The date, in UTC:

Expected output
Fri Jan 15 08:00:00 UTC 2027

What I Got​

Output
date: illegal option -- d
usage: date [-jnRu] [-I[date|hours|minutes|seconds|ns]] [-f input_fmt]
[ -z output_zone ] [-r filename|seconds] [-v[+|-]val[y|m|w|d|H|M|S]]
[[[[mm]dd]HH]MM[[cc]yy][.SS] | new_date] [+output_fmt]

A zsh terminal on macOS where date -u -d @1800000000 prints date: illegal option -- d followed by the usage message of the macOS date command.

What I Checked​

The usage message. It lists every option this date accepts: -j, -n, -R, -u, -I, -f, -z, -r and -v. There's no -d, but there is [-r filename|seconds].

The macOS manual. man date describes -r like this: "Print the date and time represented by seconds, where seconds is the number of seconds since the Epoch (00:00:00 UTC, January 1, 1970; see time(3)), and can be specified in decimal, octal, or hex."

Where -d @ comes from. It's the GNU date used on Linux. The GNU manual explains the @: "If you precede a number with '@', it represents an internal timestamp as a count of seconds."

Why It Happens​

macOS ships the BSD version of date, and Linux distributions ship GNU's, or one that behaves like it. Both print dates with the same + format codes, but they read a timestamp differently: GNU takes -d @<seconds>, and BSD takes -r <seconds>. On macOS, -d isn't an option at all, so date stops before reading the number.

The Fix​

Use -r and the number of seconds, without the @:

Terminal
date -u -r 1800000000

The format codes work the same as on Linux, so ISO 8601 is the same + string:

Terminal
date -u -r 1800000000 +%Y-%m-%dT%H:%M:%SZ

After the Fix​

Output
Fri Jan 15 08:00:00 UTC 2027
2027-01-15T08:00:00Z

The same macOS terminal after the fix: date -u -r 1800000000 prints Fri Jan 15 08:00:00 UTC 2027, and with a format string it prints 2027-01-15T08:00:00Z.

Common Mistakes​

MistakeWhat happensDo this instead
Keeping the @ with -r-r expects the number on its owndate -u -r 1800000000
Leaving out -uThe date shows in your Mac's time zoneAdd -u for UTC
Writing one script for both systems with -dIt fails on every MacCheck uname -s and use -r on Darwin, or run the script with GNU date

Troubleshooting​

SymptomLikely causeFix
date: illegal option -- dThe Linux form on macOS's dateUse -r <seconds>
The year is in the tens of thousands, such as 58870The timestamp is in millisecondsDivide 13-digit values by 1,000
The time is hours offNo -u, so it's local timeAdd -u

FAQ​

Can I get GNU date on a Mac?​

Yes. Homebrew's coreutils package installs GNU's tools with a g in front of the names macOS already uses, so GNU date becomes gdate and the built-in date keeps working.

Does -r do anything else?​

Yes. Given a file name instead of a number, it prints the time the file was last changed. That's why the usage message says -r filename|seconds.

Conclusion​

date: illegal option -- d means you ran the Linux form of date on a Mac. macOS's BSD date reads a Unix timestamp with -r <seconds>, without the @. Everything else, including -u and the + format codes, works the same.

Additional Resources​