Python
UTC Time
Datetime Module
Programming
Timezone Conversion

How to get UTC time in Python?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Getting UTC time in Python is easy, but there is an important distinction between aware and naive datetime objects. The most reliable modern approach is to create a timezone-aware UTC datetime immediately so the value can be compared, converted, and serialized without ambiguity.

Use datetime.now(timezone.utc)

The clearest built-in solution is:

python
1from datetime import datetime, timezone
2
3utc_now = datetime.now(timezone.utc)
4print(utc_now)
5print(utc_now.isoformat())

This returns a timezone-aware datetime whose tzinfo is UTC. That is the safest default for most applications because the timezone meaning travels with the value.

Typical output looks like an ISO timestamp ending in +00:00, which makes the UTC offset explicit.

Why Awareness Matters

A naive datetime has no timezone attached. It may represent local time, UTC, or something else entirely, and that ambiguity causes bugs later.

For example:

python
1from datetime import datetime
2
3naive_now = datetime.utcnow()
4print(naive_now)
5print(naive_now.tzinfo)

datetime.utcnow() returns the current UTC clock time, but the object is still naive. That means code consuming it later cannot tell from the object itself that it was intended to be UTC.

That is why datetime.now(timezone.utc) is usually preferred in new code.

Convert Existing Datetimes to UTC

If you already have a timezone-aware datetime in another timezone, convert it with astimezone():

python
1from datetime import datetime, timezone
2from zoneinfo import ZoneInfo
3
4local_time = datetime(2025, 9, 24, 9, 30, tzinfo=ZoneInfo("America/Toronto"))
5utc_time = local_time.astimezone(timezone.utc)
6
7print(local_time)
8print(utc_time)

This is the correct way to move between timezones because it preserves the actual instant in time rather than just changing the label.

Get a UTC Timestamp

Sometimes you need a Unix timestamp rather than a datetime object. You can still start from an aware UTC datetime:

python
1from datetime import datetime, timezone
2
3utc_now = datetime.now(timezone.utc)
4epoch_seconds = utc_now.timestamp()
5
6print(epoch_seconds)

If you need integer seconds instead of fractional seconds, wrap the result with int().

Format UTC Time for Output

If the goal is display or logging, format the aware datetime directly:

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

strftime() is useful here because formatting is a separate concern from obtaining the UTC time itself.

Store in UTC, Convert for Display

A common backend rule is to store timestamps in UTC and convert them to local time only at the edges of the system, such as the UI or a report. That keeps internal comparisons and database logic simpler.

For example, if an application receives user-local time, convert it once to UTC for storage. Later, convert it back to the user’s timezone only when presenting it.

This pattern avoids a surprising number of timezone bugs.

Common Pitfalls

The most common mistake is using datetime.utcnow() and assuming it is timezone-aware. It is not. The clock value is UTC, but the object itself remains naive.

Another issue is attaching UTC incorrectly to a local-time value. If you have local time and simply replace its tzinfo with UTC, you are relabeling the time rather than converting it.

People also mix naive and aware datetimes in comparisons. Python will reject many of those comparisons because the timezone meaning is unclear.

Finally, do not convert to local time too early. Keeping internal timestamps in UTC makes later logic much easier to reason about.

Summary

  • Prefer datetime.now(timezone.utc) for the current UTC time in Python.
  • Use timezone-aware datetimes so UTC intent is explicit.
  • Convert aware datetimes with astimezone(timezone.utc) when needed.
  • Use timestamp() when you need epoch seconds.
  • Store timestamps in UTC and convert only for presentation.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.