Unix Timestamp: How to Read and Convert It

by Liam Thompson
0 comment

A Unix timestamp is read by treating it as the number of seconds since 00:00:00 UTC on January 1, 1970, often called the Unix epoch [source: Wikipedia]. For example, 1717243200 means June 1, 2024, 12:00:00 UTC. Most confusion comes from three things: seconds versus milliseconds, local time zones, and timestamps stored as text instead of numbers.

TLDR: A Unix timestamp is a compact time value used by computers, APIs, logs, databases, and analytics tools. To read it, convert the number from seconds or milliseconds into a human date in UTC, then apply the needed local time zone. For example, an analytics team checking 50,000 daily events might convert 1717243200 to 2024-06-01 12:00:00 UTC to match a traffic spike with a campaign launch. If the value has 13 digits, such as 1717243200000, it is probably in milliseconds and must be divided by 1,000 first.

What a Unix timestamp means

A Unix timestamp is a plain number. It does not store a month name, a weekday, or a time zone label. It stores elapsed time from the Unix epoch.

  • 0 means 1970-01-01 00:00:00 UTC.
  • 60 means 1970-01-01 00:01:00 UTC.
  • 3600 means 1970-01-01 01:00:00 UTC.
  • -1 means 1969-12-31 23:59:59 UTC.

This simple format makes timestamps useful in software. Numbers are easy to sort. They are easy to compare. They are also compact. A database can quickly tell whether one event happened before another.

The catch is that humans do not think in raw seconds. A support agent reading 1720000000 will not instantly know the date. A conversion step is always needed.

How to read a Unix timestamp

Reading a Unix timestamp follows a short process:

  1. Check the number of digits. A 10-digit value is usually seconds. A 13-digit value is usually milliseconds.
  2. Convert it to UTC first. UTC is the safest baseline.
  3. Apply the local time zone only after conversion. This avoids off-by-hours mistakes.
  4. Confirm the source system. Some systems use microseconds or nanoseconds.

For example:

  • 1717243200 = 2024-06-01 12:00:00 UTC
  • 1717243200000 = 2024-06-01 12:00:00 UTC, after dividing by 1,000

It drives many teams crazy that tools rarely say which unit they used. One dashboard may export seconds. Another may export milliseconds. The difference can make a date appear thousands of years in the future. That tiny unit mismatch can waste 10 minutes on a bug that never existed.

Seconds, milliseconds, microseconds, and nanoseconds

Unix timestamps appear in several levels of precision. The value length gives a useful hint.

Unit Typical digits Example Conversion to seconds
Seconds 10 1717243200 No change
Milliseconds 13 1717243200000 Divide by 1,000
Microseconds 16 1717243200000000 Divide by 1,000,000
Nanoseconds 19 1717243200000000000 Divide by 1,000,000,000

Many web APIs return milliseconds because JavaScript commonly works with milliseconds. Many server logs and databases use seconds. High-speed systems, such as trading platforms or event tracing tools, may use microseconds or nanoseconds.

How to convert Unix timestamps

There are several common ways to convert a timestamp. The best option depends on the person doing the work.

Using JavaScript

JavaScript dates use milliseconds. If the timestamp is in seconds, it must be multiplied by 1,000.

const timestamp = 1717243200;
const date = new Date(timestamp * 1000);
console.log(date.toISOString());
// 2024-06-01T12:00:00.000Z

If the value is already in milliseconds, multiplication would be wrong:

const timestampMs = 1717243200000;
const date = new Date(timestampMs);
console.log(date.toISOString());

Using Python

Python can convert seconds with the datetime module.

from datetime import datetime, timezone

timestamp = 1717243200
date = datetime.fromtimestamp(timestamp, tz=timezone.utc)
print(date)
# 2024-06-01 12:00:00+00:00

For milliseconds, the program should divide by 1,000 first.

timestamp_ms = 1717243200000
date = datetime.fromtimestamp(timestamp_ms / 1000, tz=timezone.utc)

Using SQL

SQL syntax varies by database. PostgreSQL uses to_timestamp for seconds.

SELECT to_timestamp(1717243200);

MySQL can use FROM_UNIXTIME:

SELECT FROM_UNIXTIME(1717243200);

These functions may display local server time unless configured otherwise. That is where silent errors sneak in.

Time zones are the usual trap

A Unix timestamp represents one exact moment. Time zones only change how that moment is displayed. The same timestamp may show different clock times in London, New York, and Tokyo.

For example, 1717243200 is 2024-06-01 12:00:00 UTC. In New York, during daylight saving time, it appears as 2024-06-01 08:00:00. In Tokyo, it appears as 2024-06-01 21:00:00.

This matters in analytics. If a campaign starts at noon UTC but the report groups traffic by local date, conversions may look split across two days. A marketer may think the first day underperformed by 18%, when the data was only grouped under a different clock.

Common uses for Unix timestamps

Unix timestamps appear across many systems because they are simple and portable.

  • Application logs: servers record when errors, requests, and background jobs happen.
  • APIs: services pass creation times, update times, and expiration dates.
  • Databases: records store event times in a sortable numeric format.
  • Authentication: tokens often include issue and expiry timestamps.
  • Analytics: page views, clicks, purchases, and sessions use event timestamps.

They are not always the best display format. A user interface should usually show readable dates. Raw timestamps belong in storage, transfers, debugging panels, and logs.

Common mistakes when converting timestamps

Several errors appear again and again.

  • Mixing seconds and milliseconds: this is the most common problem.
  • Ignoring UTC: local time assumptions create wrong dates.
  • Storing timestamps as strings: sorting may break if formats vary.
  • Dropping precision: milliseconds may matter for event order.
  • Forgetting negative timestamps: dates before 1970 can be valid.

One more issue is the Year 2038 problem. Some older 32-bit systems cannot safely store Unix time after January 19, 2038. Modern 64-bit systems avoid this limit in normal use, but old embedded devices and legacy software may still be affected.

Best practices for storing and showing Unix time

Teams can avoid many date bugs by setting a few rules.

  • Store timestamps in UTC. Convert for display only.
  • Name fields clearly. Use names such as created_at_seconds or sent_at_ms.
  • Document units in API responses. A field named timestamp is too vague.
  • Keep original precision. Do not cut milliseconds unless there is a reason.
  • Test around daylight saving changes. These dates expose weak logic fast.

Honestly, it feels like many timestamp bugs come from one lazy field name. A clear name can save a developer from checking three logs, two dashboards, and one stale API note.

FAQ

What is a Unix timestamp?

A Unix timestamp is a number that counts time from 1970-01-01 00:00:00 UTC. It is usually counted in seconds, though some systems use milliseconds or smaller units.

How can a person tell if a Unix timestamp is in seconds or milliseconds?

A 10-digit value is usually seconds. A 13-digit value is usually milliseconds. Longer values may be microseconds or nanoseconds.

Does a Unix timestamp include a time zone?

No. It represents a single moment in time. Time zones only affect how that moment appears on a clock.

Can Unix timestamps be negative?

Yes. Negative timestamps represent dates before January 1, 1970 UTC.

Why does the same timestamp show different times in different tools?

The tools may be using different time zones, or one tool may treat the value as milliseconds while another treats it as seconds.

What is the easiest safe conversion method?

The safest method is to convert the timestamp to UTC first, confirm its unit, then display it in the required local time zone.

Related Posts