How to Convert a Unix Timestamp to a Date

Converting a timestamp to a date is one multiplication and one formatting call in any language. The difficulty is almost always the unit: seconds, milliseconds or microseconds. This guide covers both parts.

The arithmetic

Divide the timestamp by 86,400 to get the number of days since 1 January 1970. The whole part is the day count and the remainder is the time of day in seconds.

That is what the libraries do internally. Doing it by hand is useful for understanding, not for production.

Paste a timestamp and read the date.

Open the Unix Timestamp Converter →

JavaScript

new Date(1700000000 * 1000) converts seconds to a Date object. JavaScript uses milliseconds, so the multiplication is required. Then use toISOString() for UTC or toLocaleString() for local time.

If you forget the multiplication, you get a date in January 1970, which is the classic symptom.

Python

datetime.datetime.fromtimestamp(1700000000, datetime.timezone.utc) gives an aware UTC datetime. Use fromtimestamp(ts) without the zone argument for local time, which is usually what you do not want.

For a formatted string, call .strftime() with the format you need, or .isoformat() for ISO 8601.

SQL

In PostgreSQL, to_timestamp(1700000000) returns a timestamptz. In MySQL, FROM_UNIXTIME(1700000000). In SQLite, datetime(1700000000, 'unixepoch').

Milliseconds need dividing by 1000 first in most of these. Check the result against a known date before relying on it.

Excel and Sheets

Unix time counts from 1970, while spreadsheets count days from 1899 or 1904. The conversion is =timestamp/86400 + DATE(1970,1,1), formatted as a date.

The result is in UTC unless you add an offset. Add or subtract hours divided by 24 to shift to a local zone.

The unit trap

InputTreated as secondsTreated as milliseconds
170000000014 Nov 202320 Jan 1970
1700000000000Year 5584814 Nov 2023

One look at the year tells you which mistake you made. A date in 1970 means the number was seconds where milliseconds were expected; a date tens of thousands of years ahead means the reverse.

Time zones and daylight saving

A timestamp has no zone. When you format it, decide: UTC for logs and storage, a named zone for display. Never use a fixed offset for a zone that observes daylight saving, because the offset changes twice a year.

Store UTC, display local. That rule solves most of the problems people have with dates.

Common mistakes

A quick check

Convert the timestamp, then convert the date back and confirm you get the same number. If it differs by a factor of 1000, the unit was the problem. If it differs by a whole number of hours, a time zone offset crept in.

Batch converting a column

To convert a whole column in PostgreSQL, use to_timestamp(created_at) inside the select. In MySQL it is FROM_UNIXTIME(created_at). Do it in the query rather than in the application when you are exploring data, so the results are readable immediately.

If the column holds milliseconds, divide by 1000 inside the same expression. Test on a single row with a known date before running it over millions.

A conversion you can verify

Convert 0 and confirm you get 1 January 1970 at 00:00 UTC. Convert 1000000000 and confirm 9 September 2001 at 01:46:40 UTC. If both land correctly, the function and the unit are right.

Working with local time correctly

Converting to a local time means choosing a zone, not just an offset. Use a named zone such as Africa/Cairo so daylight saving is applied automatically, and keep the stored value in UTC so comparisons stay correct.

If you find yourself adding or subtracting hours by hand in application code, the design is usually wrong: format the instant in the reader zone at the point of display instead.

Frequently asked questions

How do I convert a timestamp to a date by hand?

Divide by 86,400 for days since 1970 and use the remainder for the time of day. In practice, use a library.

Why does my converted date show 1970?

The timestamp was in seconds and the function expected milliseconds, or the reverse.

Is a timestamp always UTC?

The number is an absolute instant. It becomes a local time only when formatted with a zone.

Sources

More free tools

Related guides