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.
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:
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:
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():
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:
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:
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
- How to get value of a Keras tensor in TensorFlow 2?
- How to get VirtualEnv TensorFlow to work in PyCharm?
- How to get week number in Python?
- How to get/set a pandas index column title or name?
- how to give the test size in stratified kfold sampling in python?
- How to graph grid scores from GridSearchCV?
- How to group dataframe rows into list in pandas groupby
- How to Guarantee Message delivery with Celery?
.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.