Skip to main content

How to Convert a Unix Timestamp to a Date in the Terminal

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

A Unix timestamp such as 1800000000 is easy for computers and impossible for people. One command turns it into a date you can read. The command differs between macOS, Linux and Windows, so this guide shows all three, in UTC, in the ISO 8601 format, and in any time zone.

Syntax​

On macOS, date reads a timestamp with -r:

Syntax (macOS)
date -u -r <seconds> [+<format>]

On Linux, date reads it with -d and an @ in front:

Syntax (Linux)
date -u -d @<seconds> [+<format>]
PartWhat it does
-uShows the date in UTC. Leave it out to show your computer's own time zone.
-r <seconds> (macOS) or -d @<seconds> (Linux)The timestamp to convert. The @ tells Linux's date that the number is a Unix timestamp.
+<format> (optional)How to print the date. +%Y-%m-%dT%H:%M:%SZ prints ISO 8601, such as 2027-01-15T08:00:00Z.
TZ=<zone> before the command (optional)Shows the date in another time zone, such as TZ=Asia/Tokyo.

In PowerShell:

Syntax
[DateTimeOffset]::FromUnixTimeSeconds(<seconds>).ToString('<format>')
PartWhat it does
[DateTimeOffset]::FromUnixTimeSeconds(<seconds>)Turns the timestamp into a date and time in UTC.
.ToString('u')Prints it as 2027-01-15 08:00:00Z. 'o' prints full ISO 8601 with fractions of a second.
.UtcDateTimeThe same moment as a .NET DateTime, which PowerShell prints in a long, readable form.

The Example​

The timestamp 1800000000. A JSON Web Token's exp claim, the time it stops working, is a number like this one, and reading it is the most common reason to convert a timestamp.

Step 1: Show the Date in UTC​

Terminal
date -u -r 1800000000
Output
Fri Jan 15 08:00:00 UTC 2027

1800000000 is 8:00 in the morning, UTC, on Friday 15 January 2027.

Step 2: Print It in ISO 8601​

ISO 8601, 2027-01-15T08:00:00Z, is the date format logs and APIs use: it sorts correctly and the Z says it's UTC.

Terminal
date -u -r 1800000000 +%Y-%m-%dT%H:%M:%SZ
Output
2027-01-15T08:00:00Z

%Y, %m and %d are the year, month and day; %H, %M and %S are the hours, minutes and seconds. T and Z are printed as they are.

Step 3: Show It in Another Time Zone​

Time zones have names like Asia/Tokyo or America/New_York, from the IANA time zone database.

Terminal
TZ=Asia/Tokyo date -r 1800000000
Output
Fri Jan 15 17:00:00 JST 2027

TZ=Asia/Tokyo before the command sets the time zone for that one command, and leaving out -u lets date use it.

A zsh terminal on macOS where date -u -r 1800000000 prints Fri Jan 15 08:00:00 UTC 2027, a format string prints 2027-01-15T08:00:00Z, and TZ=Asia/Tokyo prints Fri Jan 15 17:00:00 JST 2027.

Common Mistakes​

MistakeWhat happensDo this instead
Using Linux's -d @ on macOSmacOS answers date: illegal option -- dUse -r on macOS
Leaving out -uThe date shows in your computer's time zone, which may not be what you meantAdd -u for UTC
Passing millisecondsThe date lands tens of thousands of years aheadDivide by 1,000 first

Troubleshooting​

SymptomLikely causeFix
The date is hours offIt's shown in your local time zoneAdd -u, or set TZ
date: illegal option -- d on a Mac-d @ is the Linux formUse date -u -r <seconds>; the fixes post shows the error and why
PowerShell can't find a time zoneThe name is misspelledUse the full IANA name, such as Asia/Tokyo

FAQ​

Why does macOS need a different command?​

macOS uses the BSD version of date, and Linux uses a GNU-compatible one. They agree on +%s and the format codes, but not on how to read a timestamp: BSD uses -r, GNU uses -d @.

How do I read a JWT's expiry this way?​

Decode the token's payload, take the number after "exp":, and convert it as above. How to Decode a JWT in the Terminal shows the decoding.

How do I get the current timestamp?​

How to Get the Current Unix Time in the Terminal shows it for each system.

Conclusion​

To turn a Unix timestamp into a date, use date -u -r <seconds> on macOS, date -u -d @<seconds> on Linux, and [DateTimeOffset]::FromUnixTimeSeconds(<seconds>) in PowerShell. Add a format for ISO 8601, and TZ= or ConvertTime for another time zone. 1800000000 is 8:00 UTC on 15 January 2027, wherever you run it.

Additional Resources​