Convert python datetime to epoch with strftime
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
When converting a Python datetime to Unix epoch seconds, strftime() is usually not the tool you actually want. strftime() formats dates as strings, while epoch conversion is best handled with datetime.timestamp(), provided you are explicit about time zones.
What Epoch Time Means
Unix epoch time counts the number of seconds since 1970-01-01 00:00:00 in UTC. That definition is simple, but conversions become error-prone if your datetime object is naive, meaning it has no time zone attached.
A timezone-aware datetime is much safer because Python can translate it to UTC unambiguously.
Use timestamp() Instead of strftime()
The direct way to convert a datetime to epoch seconds is:
This returns a floating-point number because it can include fractional seconds.
If you want an integer Unix timestamp:
That is usually the cleanest answer.
Why strftime() Is Confusing Here
Developers often reach for strftime('%s') because some Unix-like systems support it. The problem is that %s is not portable Python behavior across platforms, and it still treats the result as formatted output rather than a first-class timestamp conversion.
For example, this may work on one machine and fail or behave differently on another:
Because of that portability issue, timestamp() is the preferred solution in Python code.
Handle Naive Datetimes Carefully
A naive datetime has no timezone information:
This may still produce a number, but Python interprets the naive value in the local system timezone. That means the same code can produce different epoch values on different machines.
If the value is meant to represent UTC, attach UTC explicitly:
If the naive value represents local time and you need a reliable UTC conversion, convert it deliberately instead of assuming the environment is correct by accident.
Convert from Strings Before Converting to Epoch
Often the real workflow is string to datetime to epoch. Use strptime() to parse the input first:
Here strptime() parses the string, and timestamp() performs the actual epoch conversion.
When strftime() Is Still Useful
strftime() is still useful after conversion if you want to display a human-readable time for debugging or validation:
That makes strftime() a formatting tool around the conversion process, not the conversion mechanism itself.
Common Pitfalls
The biggest mistake is using strftime('%s') and assuming it is portable Python. It is not a dependable cross-platform solution.
Another issue is converting naive datetimes without thinking about timezone meaning. Local timezone assumptions can silently change the result.
People also truncate timestamps too early. If sub-second precision matters, keep the floating-point result instead of forcing int() immediately.
Finally, do not confuse strptime() and strftime(). The first parses strings into datetime values, and the second formats datetime values into strings.
Summary
- Use
datetime.timestamp()for epoch conversion in Python. - Treat
strftime()as a formatting tool, not the primary conversion API. - Prefer timezone-aware
datetimevalues so the conversion is unambiguous. - Be careful with naive datetimes because local timezone assumptions affect the result.
- Use
strptime()to parse input strings before converting them to epoch seconds.

