Python
datetime
epoch
strftime
tutorial

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:

python
1from datetime import datetime, timezone
2
3dt = datetime(2025, 9, 23, 12, 30, 0, tzinfo=timezone.utc)
4epoch_seconds = dt.timestamp()
5
6print(epoch_seconds)

This returns a floating-point number because it can include fractional seconds.

If you want an integer Unix timestamp:

python
epoch_seconds = int(dt.timestamp())
print(epoch_seconds)

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:

python
1from datetime import datetime
2
3dt = datetime.now()
4print(dt.strftime('%s'))

Because of that portability issue, timestamp() is the preferred solution in Python code.

Handle Naive Datetimes Carefully

A naive datetime has no timezone information:

python
1from datetime import datetime
2
3dt = datetime(2025, 9, 23, 12, 30, 0)
4print(dt.timestamp())

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:

python
1from datetime import datetime, timezone
2
3dt = datetime(2025, 9, 23, 12, 30, 0, tzinfo=timezone.utc)
4print(int(dt.timestamp()))

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:

python
1from datetime import datetime, timezone
2
3raw = "2025-09-23 12:30:00"
4dt = datetime.strptime(raw, "%Y-%m-%d %H:%M:%S").replace(tzinfo=timezone.utc)
5
6print(int(dt.timestamp()))

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:

python
1from datetime import datetime, timezone
2
3dt = datetime.fromtimestamp(1758630600, tz=timezone.utc)
4print(dt.strftime("%Y-%m-%d %H:%M:%S %Z"))

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 datetime values 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.

Course illustration
Course illustration

All Rights Reserved.