Converting Epoch time into the datetime
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Epoch time is a count of elapsed seconds from 1970-01-01 00:00:00 UTC. Converting it into a readable datetime is straightforward, but many bugs come from using the wrong unit, the wrong timezone, or the wrong API for local versus UTC output.
Know Whether the Number Is Seconds, Milliseconds, or More
The first question is not how to convert the timestamp. It is what the number means.
These values represent different units:
- '
1700000000is roughly seconds since the Unix epoch' - '
1700000000000is roughly milliseconds' - '
1700000000000000is roughly microseconds'
If you treat milliseconds as seconds, you will get a date far in the future or an out-of-range error.
Convert Epoch Time in Python
Python's datetime module is enough for most conversions. Use datetime.fromtimestamp for local time and datetime.utcfromtimestamp or a timezone-aware variant for UTC.
If your input is in milliseconds, divide by 1000 first:
That produces a timezone-aware datetime, which is usually safer than a naive one.
Convert to a Specific Time Zone
UTC is a good storage format, but applications often need local display. In modern Python, zoneinfo is the standard way to do that.
This is better than manually subtracting hours because daylight saving rules are handled for you.
Parse Many Values Safely
If you are processing logs or CSV files, add a unit check instead of assuming every input is seconds.
That kind of guard prevents silent data corruption when timestamps come from multiple systems.
Convert in SQL and JavaScript Too
If the source data is not in Python, the same concept still applies.
In PostgreSQL:
For milliseconds in PostgreSQL:
In JavaScript, the Date constructor expects milliseconds:
That difference is worth remembering because Python often works with seconds while JavaScript often works with milliseconds.
Formatting the Result
Readable output matters just as much as conversion. In Python, isoformat() is a good default because it is unambiguous.
Use ISO 8601 for APIs and logs. Use strftime only when you need a human-facing format.
Why Time Zone Awareness Matters
A naive datetime has no timezone attached. That is fine for quick printing, but it becomes dangerous when values move between systems or get compared to timezone-aware values.
A better rule is:
- store and compare in UTC
- attach a timezone explicitly
- convert to local time only at presentation time
That keeps business logic predictable.
Common Pitfalls
Treating milliseconds as seconds. This is the most common cause of obviously wrong output.
Using local time when you expected UTC. fromtimestamp without a timezone argument uses the local machine's timezone.
Creating naive datetimes and comparing them with aware datetimes. Python will either raise or produce confusing code paths.
Hard-coding timezone offsets. Use zoneinfo instead of subtracting a fixed number of hours.
Formatting before you normalize the timezone. Convert first, then render the string.
Summary
- Epoch time is usually stored as seconds since
1970-01-01 00:00:00 UTC. - Always confirm whether the input is seconds, milliseconds, or microseconds.
- In Python, prefer timezone-aware conversions with
timezone.utc. - Use
zoneinfofor local display instead of manual hour offsets. - For APIs and logs,
isoformat()is usually the safest output format.

