How to convert a date to Unix epoch time in Python?
Quick answer
Convert a datetime to Unix epoch seconds with dt.timestamp() in Python 3, for example datetime(2024,1,1).timestamp(). Be explicit about timezone: a naive datetime is interpreted as local time, so attach tzinfo=timezone.utc first if you mean UTC. To go the other way, use datetime.fromtimestamp(ts, tz=timezone.utc).
Short answer: Call dt.timestamp() on a datetime — it returns Unix epoch seconds as a float (Python 3.3+). The catch is timezones: a naive datetime (no tzinfo) is read as local time, so if you mean UTC, attach tzinfo=timezone.utc first. Reverse it with datetime.fromtimestamp(ts, tz=timezone.utc).
Unix epoch time is the number of seconds since 1970-01-01 00:00:00 UTC. The whole subtlety in converting a date is which timezone Python assumes for a datetime that doesn't carry one.
The right way: datetime.timestamp()
from datetime import datetime, timezone
# UTC — be explicit, and the result is unambiguous
dt = datetime(2024, 1, 1, tzinfo=timezone.utc)
print(dt.timestamp()) # 1704067200.0timestamp() returns a float (seconds, with microsecond precision). Wrap it in int() if you only want whole seconds:
epoch = int(dt.timestamp()) # 1704067200The timezone trap (why your value is off by hours)
A datetime with no tzinfo is naive, and timestamp() interprets it as your machine's local time — not UTC. That is the single most common bug here:
naive = datetime(2024, 1, 1) # no timezone attached
naive.timestamp() # depends on the server's local TZ!On a machine set to UTC you'd get 1704067200.0; on one set to America/New_York (UTC-5) you'd get 1704085200.0 — a five-hour difference. If your date represents UTC, say so:
utc = datetime(2024, 1, 1, tzinfo=timezone.utc)
utc.timestamp() # always 1704067200.0, anywhereRule of thumb: make the datetime timezone-aware before converting. Naive datetimes are the reason "my epoch is off by a few hours" questions exist.
Converting back
from datetime import datetime, timezone
ts = 1704067200
datetime.fromtimestamp(ts, tz=timezone.utc) # 2024-01-01 00:00:00+00:00Pass tz=timezone.utc here too. Without it, fromtimestamp gives you a local-time datetime, reintroducing the same ambiguity on the way out.
What about time.mktime()?
You'll see mktime(dt.timetuple()) in older answers. It works, but it has two real drawbacks: it assumes local time (never UTC) and it discards sub-second precision because timetuple() drops microseconds. Prefer timestamp(). If you specifically need a UTC epoch from a struct_time, use calendar.timegm() instead of mktime():
import calendar
calendar.timegm(datetime(2024, 1, 1).utctimetuple()) # 1704067200Current epoch, right now
For "seconds since the epoch, this instant" you don't need a datetime at all:
import time
time.time() # float epoch seconds now
from datetime import datetime, timezone
datetime.now(timezone.utc).timestamp() # equivalentRelated guides
Sources
Key takeaways
- •datetime.timestamp() (Python 3.3+) returns Unix epoch seconds directly.
- •A naive datetime is read as LOCAL time; attach tzinfo=timezone.utc first if you mean UTC.
- •time.mktime(dt.timetuple()) also works but is local-time only and unavailable on some platforms.
- •Reverse the conversion with datetime.fromtimestamp(ts, tz=timezone.utc).
Frequently asked questions
What is the simplest way to get a Unix timestamp in Python?
Call dt.timestamp() on a datetime object; it returns epoch seconds as a float in Python 3.3+.
Why is my epoch time off by a few hours?
A naive datetime is interpreted as local time, so the offset is your timezone. Set tzinfo=timezone.utc on the datetime before calling timestamp() to get a UTC epoch.
mktime or timestamp - which should I use?
Prefer dt.timestamp(); it is timezone-aware and cross-platform. time.mktime assumes local time and is not available everywhere.
Software Engineering Leader & Technical Author · Updated September 9, 2026