Unix Timestamps and Time Zones
Learn what a Unix timestamp measures, how to convert it, and how to stop time zone bugs.
Key takeaways
- A Unix timestamp counts seconds from 1970-01-01 00:00:00 UTC and means the same instant in every location.
- Check the unit before converting, because many languages and APIs use milliseconds rather than seconds.
- Store instants in UTC or as timestamps, and apply a named time zone only when you display them.
- For future events tied to a place, save the local time plus the zone name, since offset rules can change.
- Systems that hold time in a signed 32-bit number run out of room in January 2038.
A Unix timestamp is the number of seconds that have passed since midnight UTC on January 1, 1970. It identifies one instant that is the same everywhere on Earth. It carries no time zone, because the time zone only matters when you turn that number into a date and clock reading a person can understand.
Most time bugs come from mixing up those two things: the instant, and the way a particular place writes it down. Once you keep them separate, converting, storing and comparing times gets much easier.
What the number counts
The starting point, January 1, 1970 at 00:00:00 UTC, is called the Unix epoch. UTC, Coordinated Universal Time, is the reference clock that civil time zones are defined against. A timestamp of 0 is that starting instant, and 86400 is exactly one day later, because a day has 86,400 seconds.
Negative values describe moments before 1970. Many systems handle them, but some libraries and database column types don't, so test before relying on them for birth dates or historical records. A plain calendar date type is usually a better fit for those anyway.
Unix time treats every day as exactly 86,400 seconds long. The official time scale has occasionally had a leap second inserted to stay close to the Earth's rotation, and Unix time doesn't count it. For almost all web and application work you can ignore this. If you measure very precise intervals across such an event, you need a clock designed for that job.
Computers learn the current time from a network service, usually through NTP. A timestamp is only as good as the clock that produced it, so two servers with drifting clocks can disagree about which event happened first.
Seconds, milliseconds and friends
The classic Unix timestamp is in whole seconds. JavaScript's Date.now() returns milliseconds. Some logging and database systems use microseconds or nanoseconds. The number itself doesn't say which, so you infer it from size and documentation.
For dates in the current era, a value in seconds has 10 digits and a value in milliseconds has 13. If you pass milliseconds to something expecting seconds, you get a date tens of thousands of years in the future. The reverse mistake lands you in January 1970.
Name your fields so the unit is obvious. created_at_ms or expires_at_seconds saves the next developer from guessing.
Converting on the command line and in code
You can convert quickly without writing a program. The commands differ by system.
# Linux (GNU date)
date +%s
date -u -d @1700000000
# macOS and BSD
date +%s
date -u -r 1700000000 # Windows PowerShell
[DateTimeOffset]::UtcNow.ToUnixTimeSeconds()
[DateTimeOffset]::FromUnixTimeSeconds(1700000000) In JavaScript, multiply seconds by 1000 before creating a date.
new Date(1700000000 * 1000).toISOString()
// 2023-11-14T22:13:20.000Z
Math.floor(Date.now() / 1000)
// current time in seconds If you'd rather paste a value into a page, the timestamp converter shows both directions, and the date calculator helps with gaps between dates.
Time zones and offsets
An offset is a difference from UTC, such as +05:30 or -08:00. A time zone is a set of rules that says which offset applies in a region at each moment, including past changes and daylight saving time.
Those are different things. -05:00 tells you how one reading relates to UTC. America/New_York tells you that the region uses -05:00 for part of the year and -04:00 for the rest, and also what it did in earlier years. Names in that style come from the IANA time zone database, which operating systems and programming languages ship and update.
Avoid three-letter abbreviations in stored data. Several are ambiguous: CST can refer to more than one zone in different parts of the world. Offsets or IANA names remove the doubt.
Governments change their time rules, sometimes with little notice. That's why the zone database receives updates, and why an old, unpatched system can show the wrong local time for a region even though its clock is right in UTC.
Daylight saving traps
Where daylight saving time applies, local clocks jump. In spring, an hour of local time doesn't exist. In autumn, an hour happens twice.
Say a job is scheduled for 2:30 a.m. local time in a region whose clocks move forward from 2:00 to 3:00. On that night, 2:30 never occurs. On the autumn night when clocks fall back, an affected time occurs twice, and a naive scheduler may run the job twice. Schedulers handle this differently, which is worth checking if you rely on cron jobs. Running servers in UTC avoids the whole problem for machine tasks.
Daylight saving also means that "add one day" and "add 24 hours" can give different local results. If a reminder should arrive at 9 a.m. tomorrow, add a calendar day in the user's zone. If a token should expire after 24 hours, add 86,400 seconds to the timestamp.
Storing time in databases and APIs
For events that already happened, the safe default is to record the instant in UTC. A timestamp integer does this naturally. So does a database type that is defined to hold an instant. Read your database's documentation carefully, because column types with similar names behave differently: some convert to and from the session's time zone, while others store exactly the digits you gave them with no zone at all.
For text exchanged between systems, such as in JSON, use the RFC 3339 profile of ISO 8601:
2026-03-15T14:30:00Z
2026-03-15T09:30:00-05:00 The Z means UTC. The second line describes a local reading that is five hours behind UTC, and it refers to the same instant as the first. Both are unambiguous. A string like 2026-03-15 14:30 with no zone or offset forces the reader to guess.
Text in this format also sorts correctly as a string when every value uses the same offset and precision. Mixed offsets break that, which is one more reason to normalize to UTC.
Future events are the exception. If someone books an appointment for 10:00 in a given city next year and the region changes its rules in between, a precomputed UTC value would now point to the wrong wall-clock time. Keeping the local time and the zone name lets you recalculate the instant with updated rules.
Showing times to people
Convert as late as possible, ideally in the layer that talks to the user. Browsers know the visitor's time zone, and the built-in internationalization functions can format a timestamp accordingly:
new Date(1700000000000).toLocaleString("en-US", {
timeZone: "Europe/Paris",
timeZoneName: "short"
}) When the time matters, say which zone you're showing. "The sale ends at 11:59 p.m." causes support requests; adding the zone prevents them. For coordinating across regions, a time zone converter is quicker than mental arithmetic.
Relative phrases such as "3 hours ago" avoid zones altogether, but offer the exact time on hover or tap so people can verify it.
The year 2038 limit
A signed 32-bit integer can hold values up to 2,147,483,647. As a count of seconds since the epoch, that is reached at 03:14:07 UTC on January 19, 2038. One second later, such a counter wraps around to a large negative number that reads as a date in December 1901.
Modern 64-bit operating systems generally use 64-bit time, which pushes the limit far beyond any practical concern. The risk sits in older embedded devices, old file formats, and database columns or program variables that are still 32 bits. Software that calculates future dates, such as long loans or long-lived certificates, can hit the problem well before 2038.
If you maintain older code, search for integer types used to hold times and widen them to 64 bits.
Debugging a wrong time
Start by finding the earliest place where the value is still correct. Print the raw number or string as it arrives from the client, as it's written to storage, as it's read back and as it's shown. The step where it changes is where the bug lives.
Next, compare the size of the error with the likely causes. A shift of exactly one hour often points at daylight saving rules. A shift equal to a zone's offset means something treated local time as UTC, or the reverse. A difference of a few seconds or minutes suggests an unsynchronized clock. A date that is off by one day usually means a calendar date was converted through a time zone when it should have been left alone.
A checklist for bug-free time handling
- Decide whether each value is an instant, a local appointment, a date or a duration.
- Put the unit in numeric field names.
- Keep servers and logs in UTC.
- Use IANA zone names for people and places, never abbreviations.
- Keep operating system and library time zone data updated.
- Test around daylight saving changes, month ends and February 29.
- Don't hand-write date arithmetic when a well-maintained library or built-in function can do it.
One last habit helps when debugging: write down the raw value, the unit you believe it uses, and the zone you're viewing it in. If a time is off by a whole number of hours, suspect a zone conversion applied twice or not at all. If it's wildly wrong, suspect the unit.