HOWTO · Python

How to Convert Epoch to Datetime in Python

It introduces how to convert epoch to date time in Python

On this page

Unix epoch time is a count of seconds from 1970-01-01 00:00:00 UTC. A timestamp such as 1606262400 identifies an instant; converting it to a readable date requires choosing a time zone. Use datetime when you want a date-and-time object, or time when a struct_time value suits your task.

Convert Epoch Seconds with datetime.fromtimestamp()

The Python datetime module provides datetime.fromtimestamp(), which accepts a timestamp and returns a datetime object. By default, it interprets the timestamp in the machine’s local time zone and returns a naive datetime, which does not carry time-zone information. Pass timezone.utc to get an aware datetime representing UTC.

Run this example:

from datetime import datetime, timezone

timestamp = 1606262400
utc_datetime = datetime.fromtimestamp(timestamp, timezone.utc)
print(utc_datetime)

For a shorter import when you use the class repeatedly, write from datetime import datetime and call datetime.fromtimestamp(...). The fromtimestamp() reference documents its arguments and platform behavior. Keep timestamps in UTC when exchanging instants between systems, then convert to a display time zone at the application boundary.

Convert Epoch Seconds with the time Module

The Python time module offers two useful conversions. time.localtime() returns a nine-field struct_time interpreted in the host’s local time zone. time.gmtime() returns the corresponding UTC-based struct_time. These are structured time values, not datetime objects.

import time

timestamp = 1554723620
local_value = time.localtime(timestamp)
utc_value = time.gmtime(timestamp)
print("Local:", local_value)
print("UTC:", utc_value)

The local line depends on the system time-zone settings, so its clock fields can differ from the UTC line. Use time.gmtime() when you need stable UTC fields, and time.localtime() when you specifically need the host’s local representation.

Format the Converted Date and Time

Both time values and datetime objects provide strftime() for formatting. Directives such as %Y, %m, %d, %H, %M, and %S represent the year, month, day, hour, minute, and second. The %d directive is portable for a zero-padded day of the month. The %-d variant omits leading zeroes on some platforms, but is not supported everywhere.

import time

timestamp = 1554723620
utc_value = time.gmtime(timestamp)
print(time.strftime("%Y-%m-%d %H:%M:%S", utc_value))

You can use datetime.strftime() with the same numeric directives. Directives that produce names, such as %A and %B, and the locale-oriented %c can vary with the system locale; do not rely on their wording for machine-readable data. For a formatting reference, see strftime.org.

Handle UTC, Time Zones, and Millisecond Timestamps

A timestamp represents seconds relative to the UTC epoch, but local conversion uses the machine’s configured time zone. Prefer an aware UTC value for storage and calculations that represent a real instant. To display it elsewhere, convert the aware value with astimezone(). A naive value from the default datetime.fromtimestamp() does not identify its time zone, so avoid treating it as UTC unless you know that was the intended interpretation.

Some APIs return milliseconds instead of seconds. A millisecond timestamp is typically much larger than a contemporary seconds timestamp; divide it by 1000 before passing it to either conversion API. Preserve the fractional part if sub-second precision matters.

from datetime import datetime, timezone

timestamp_ms = 1606262400123
timestamp_seconds = timestamp_ms / 1000
utc_datetime = datetime.fromtimestamp(timestamp_seconds, timezone.utc)
print(utc_datetime)

The final value includes the fractional second represented by the input. For local display, call utc_datetime.astimezone(); the resulting clock time depends on the host’s configured zone. Keep the original numeric timestamp when you need to preserve the instant independently of display formatting.