← All notes

Dates & time

What Unix timestamps mean

Check units and time zones before converting a timestamp.

A Unix timestamp represents elapsed time from January 1, 1970 at 00:00:00 UTC, conventionally in seconds and ignoring leap seconds. Some systems use milliseconds instead, including JavaScript's Date constructor.

One instant, two units

These sample values represent the same instant:

1704067200 seconds
1704067200000 milliseconds
2024-01-01T00:00:00.000Z

The Z marks UTC. To convert seconds to milliseconds, multiply by 1000. To convert milliseconds to seconds, divide by 1000; whether to keep or round a fraction depends on the receiving system.

Make the units explicit

const seconds = 1704067200;
const instant = new Date(seconds * 1000);
instant.toISOString();
// "2024-01-01T00:00:00.000Z"

Passing the seconds value directly to new Date() interprets it as milliseconds and produces a date near the start of 1970. An unexpected 1970 date is a useful clue to a unit mismatch.

Separate the instant from its display

The same timestamp can appear as different clock times in different time zones. A local date may even be the previous or next calendar day. When comparing records, use an explicit time zone or UTC.

For an unfamiliar field, check its documentation and test a known date. The number of digits can offer a clue for contemporary timestamps, but it is not a reliable unit declaration for every value.

Further reading