Converting datetime.date to UTC timestamp in Python
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
A datetime.date object represents a calendar date without a time or timezone. Converting it to a UTC timestamp (seconds since 1970-01-01 00:00:00 UTC) requires deciding what time of day and what timezone the date represents. The standard approach is to treat the date as midnight UTC, combine it with time(0, 0) and timezone.utc, then call .timestamp(). Getting this wrong is the most common source of timestamp bugs in Python.
The Direct Approach (Python 3.3+)
Why date Alone Is Ambiguous
datetime.timestamp() on a naive datetime assumes local time. This means the same code produces different timestamps on different machines.
Using calendar.timegm()
calendar.timegm() is the UTC equivalent of time.mktime(). It always interprets the input as UTC regardless of the system timezone.
Using time.mktime() (Local Time, Usually Wrong)
time.mktime() converts local time to a timestamp. If you want UTC, do not use this. Use calendar.timegm() instead.
Integer vs Float Timestamps
Converting Back (Timestamp to date)
With Timezone-Aware Dates
The same calendar date corresponds to different UTC timestamps depending on the timezone.
Pandas Integration
Epoch Reference
Common Pitfalls
- Naive datetime assumes local time:
datetime.combine(d, time()).timestamp()uses the system's local timezone, not UTC. Always passtzinfo=timezone.utcexplicitly. - Date boundary errors: A timestamp of
1758672000(midnight UTC Sep 24) becomes Sep 23 at 7 PM in US Eastern. Always specify UTC when converting timestamps back to dates. datetime.utcnow()is not UTC-aware: Despite the name,datetime.utcnow()returns a naive datetime. Usedatetime.now(timezone.utc)instead (Python 3.2+).- DST ambiguity: During the fall-back DST transition, the same local time occurs twice. Using
mktime()during this period may produce the wrong timestamp. UTC has no DST. - 32-bit timestamp overflow: Timestamps stored as 32-bit integers overflow on January 19, 2038. Use 64-bit integers or Python's arbitrary-precision
int.
Summary
- Use
datetime.combine(d, time(), tzinfo=timezone.utc).timestamp()for date-to-UTC-timestamp - Use
calendar.timegm(d.timetuple())as an alternative that always treats input as UTC - Never use
time.mktime()for UTC conversions because it uses local time - Always pass
tz=timezone.utctodatetime.fromtimestamp()when converting back - The same calendar date maps to different timestamps in different timezones
- Use
int(ts * 1000)for millisecond timestamps (JavaScript/Java-style)
Related reading
- Converting dictionary to JSON
- Converting Dictionary to List?
- Converting Epoch time into the datetime
- Converting from a string to boolean in Python
- Converting from a string to boolean in Python
- Converting integer to binary in Python
- Converting JSON String to Dictionary Not List
- Converting LinearSVC's decision function to probabilities Scikit learn python
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.