24 Oct Unix Time: How to Convert and Use Timestamps
Unix time is the safest common format for storing moments in software because it reduces a date to one number: the count of seconds since 1970-01-01 00:00:00 UTC, not including leap seconds [source: Wikipedia]. A timestamp such as 1704067200 means 2024-01-01 00:00:00 UTC. That plain number is easy to sort, compare, store, send through APIs, and index in databases.
TLDR: Unix time stores a point in time as seconds, or sometimes milliseconds, from 1 January 1970 in UTC. For example, 1704067200 converts to 2024-01-01 00:00:00 UTC. In a typical analytics setup, a team processing 2 million events per day may store all event times as Unix timestamps, then display local time only in dashboards. One product team could cut support tickets about “wrong times” by 35% just by storing UTC timestamps and converting only at display time.
What Unix Time Means
Unix time is also called epoch time or a Unix timestamp. It is a single integer that marks elapsed seconds from the Unix epoch. The epoch is midnight on 1 January 1970 in UTC.
This format avoids many messy date problems. It does not care about month names. It does not care about local date formats. It does not care whether a user writes 03/04/2026 and means March 4 or April 3. That is why servers, APIs, logs, databases, payment tools, and analytics systems use it so often.
The catch is that Unix time is not friendly to human eyes. A support agent can read 2026-10-23 14:15:00. Almost nobody can glance at 1792764900 and know what happened. So Unix time works best as a storage and exchange format, not as a display format.
Seconds vs Milliseconds
Unix timestamps often appear in two common lengths:
- 10 digits: seconds, such as
1704067200 - 13 digits: milliseconds, such as
1704067200000
This difference causes a lot of annoying bugs. A timestamp in milliseconds treated as seconds may produce a date tens of thousands of years in the future. A timestamp in seconds treated as milliseconds may show a date in January 1970. It drives developers a little mad because the mistake often looks like a timezone issue at first.
A simple rule helps:
- If the number has about 10 digits, it is probably seconds.
- If the number has about 13 digits, it is probably milliseconds.
- If the number has 16 digits, it may be microseconds.
- If the number has 19 digits, it may be nanoseconds.
How to Convert Unix Time to a Date
Conversion means taking the numeric timestamp and rendering it as a readable date. The key question is always the same: which timezone should be used for display?
For example:
- Unix timestamp:
1704067200 - UTC date:
2024-01-01 00:00:00 UTC - New York time:
2023-12-31 19:00:00 EST - London time:
2024-01-01 00:00:00 GMT
The timestamp did not change. Only the presentation changed. This is the heart of using Unix time well.
Most programming languages include built-in tools for conversion:
- JavaScript: multiply seconds by 1000 before creating a
Dateobject. - Python: use
datetime.fromtimestamp()ordatetime.utcfromtimestamp(). - PHP: use
date()with the timestamp. - SQL: use functions such as
FROM_UNIXTIME()in MySQL orto_timestamp()in PostgreSQL.
How to Convert a Date to Unix Time
Going the other way requires a date, a time, and usually a timezone. A system takes that readable value and calculates how many seconds have passed since the epoch.
Example:
- Input:
2024-01-01 00:00:00 UTC - Output:
1704067200
If the input is local time, the timezone must be clear. 2024-01-01 00:00:00 in Tokyo is not the same instant as 2024-01-01 00:00:00 in Berlin. Without timezone data, software may guess. That is where bugs start.
Many systems also use ISO 8601 strings for readable exchange, such as 2024-01-01T00:00:00Z. The Z means UTC. RFC 3339, often used by APIs, is based on that style [source: RFC Editor].
Common Uses for Unix Timestamps
Unix time is common because computers compare numbers quickly. That sounds dull, but it matters at scale.
- Log files: servers record when each request happened.
- Analytics: events are grouped by minute, hour, day, or week.
- Authentication: tokens can expire after a set timestamp.
- Databases: rows can be sorted by creation or update time.
- Caching: systems can decide when stored data is stale.
- Scheduling: jobs can run before or after a fixed instant.
For analytics, timestamps are especially useful. A team can record signup_time, first_purchase_time, and last_login_time as Unix timestamps. Then it can calculate time to conversion. If 10,000 new users sign up and 2,400 purchase within 48 hours, the 48-hour conversion rate is 24%. No custom date parsing is needed once the data is clean.
Best Practices
Strong timestamp handling usually follows a few simple rules:
- Store time in UTC. Local time should be used for display, not storage.
- Record the unit. Label fields as seconds, milliseconds, or another unit.
- Use integers when possible. Floating-point time can create tiny precision errors.
- Convert at the edge. APIs and databases can store UTC, while apps show local time.
- Test daylight saving changes. Some regions skip or repeat local hours.
- Avoid silent guesses. If timezone data is missing, the system should fail clearly.
Honestly, it feels like half of timestamp pain comes from hidden assumptions. One service sends milliseconds. Another expects seconds. A browser renders local time. A database stores UTC. The fix is not fancy. The fix is clear field names, clear units, and boring tests.
Image not found in postmeta
Common Mistakes
The most common mistake is mixing seconds and milliseconds. The second is ignoring timezones. The third is showing UTC to users who expect local time.
Another problem is treating a date without a time as a full timestamp. A birthday, for example, is not the same type of data as a login event. A birthday should not shift to the previous day because a timezone conversion ran at midnight UTC. Dates and timestamps solve different problems.
There is also the 2038 problem. Older 32-bit systems can run out of range for signed Unix timestamps in January 2038. Modern 64-bit systems avoid this for ordinary business use, but old embedded systems and legacy software may still need review.
FAQ
What is Unix time?
Unix time is a numeric count of seconds since 1970-01-01 00:00:00 UTC. It is used to represent a specific instant.
Is Unix time always in UTC?
The timestamp itself represents time relative to UTC. Displayed output may appear in any timezone.
How can a timestamp be converted to a normal date?
A converter, programming language, database function, or operating system tool can transform the number into a readable date. The display timezone must be chosen.
Why do some timestamps have 13 digits?
Thirteen-digit timestamps usually use milliseconds. Ten-digit timestamps usually use seconds.
Should databases store Unix timestamps or date types?
Both can work. Many teams prefer native timestamp types for database features, while APIs often send Unix timestamps. The main rule is to store UTC and document units.
What is the biggest risk when using Unix time?
The biggest risk is mixing units or timezones. Clear names such as created_at_utc_ms can prevent many errors.
Sorry, the comment form is closed at this time.