Skip to main content

How to Get the Current Unix Time in the Terminal

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

Unix time is a single number: the seconds since midnight UTC on 1 January 1970. Logs, databases and JSON Web Tokens use it because it's the same everywhere, with no time zones or date formats to argue about. One short command prints it in any terminal.

Syntax​

On macOS and Linux:

Syntax
date +%s
PartWhat it does
datePrints the date and time.
+%sPrints them in the format that follows the +. %s stands for the seconds since 1970-01-01 00:00:00 UTC.

In PowerShell:

Syntax
[DateTimeOffset]::UtcNow.ToUnixTimeSeconds()
PartWhat it does
[DateTimeOffset]::UtcNowThe current date and time in UTC.
.ToUnixTimeSeconds()Turns it into Unix time. .ToUnixTimeMilliseconds() gives milliseconds instead.

The Example​

A sign-in token records when it was made and when it stops working, both as Unix time. RFC 7519, the JWT standard, calls this a NumericDate: "the number of seconds from 1970-01-01T00:00:00Z UTC until the specified UTC date/time, ignoring leap seconds." The example prints the time now, then the time one hour from now, which is what a one-hour token stores as its expiry.

Step 1: Print the Current Unix Time​

Terminal
date +%s
Output
1795599739

Your number will be larger: it goes up by one every second. It doesn't depend on your time zone, so two computers in different countries print the same value at the same moment.

Step 2: Add Time to It​

Unix time is just a number of seconds, so a time in the future is plain addition. An hour is 3,600 seconds:

Terminal
echo $(( $(date +%s) + 3600 ))
Output
1795603339

$(date +%s) runs date and puts its output in place, and $(( … )) does the arithmetic.

Step 3: Get Milliseconds​

JavaScript and some APIs count in milliseconds instead of seconds. Read as seconds, a millisecond value is a date tens of thousands of years in the future, so check which one a system expects.

Terminal
echo $(( $(date +%s%N) / 1000000 ))
Output
1795599739893

%N adds the nanoseconds, nine more digits, and dividing by 1,000,000 leaves milliseconds. I checked %N on macOS 26 and on Ubuntu 26.04.

A zsh terminal where date +%s prints 1795599739, adding 3600 prints 1795603339, and date +%s%N divided by 1000000 prints the time in milliseconds, 1795599739893.

Common Mistakes​

MistakeWhat happensDo this instead
Putting milliseconds where seconds are expectedDates land tens of thousands of years in the futureJWTs and date +%s use seconds; divide milliseconds by 1,000
Writing the expiry by handIt's easy to drop a digitAdd seconds to the current time, as in Step 2

Troubleshooting​

SymptomLikely causeFix
The number has 13 digits instead of 10It's in millisecondsDivide by 1,000
date +%s%N doesn't print 19 digitsYour date doesn't support %NUse seconds, or PowerShell's ToUnixTimeMilliseconds()
A date shown from the number is hours offIt was shown in your local time zoneShow it in UTC; see the guide below

FAQ​

Does Unix time depend on my time zone?​

No. It counts seconds since a fixed moment in UTC, so it's the same everywhere. Only turning it into a readable date involves a time zone.

How do I turn the number back into a date?​

How to Convert a Unix Timestamp to a Date in the Terminal shows it on macOS, Linux and in PowerShell.

What about leap seconds?​

Unix time ignores them, and so does the JWT standard, in the definition quoted above.

Conclusion​

date +%s prints the current Unix time on macOS and Linux, and [DateTimeOffset]::UtcNow.ToUnixTimeSeconds() prints it in PowerShell. Add seconds for a time in the future, such as 3,600 for an hour, and use milliseconds only where a system asks for them.

Additional Resources​