How to Convert Local Time to UTC in Python

If I have a local time in Python and need the same moment in UTC, I convert it with astimezone(timezone.utc). In this tutorial, I’ll walk through the safest way to do it, why timezone-aware datetimes matter, and how I use this in real code.

Why this matters

I use UTC whenever I need a time value that stays consistent across machines, cities, and daylight saving changes. Local time is great for display, but UTC is much safer for storage, logging, APIs, and scheduling.

If I keep local time everywhere, I can end up with hard-to-find bugs when users are in different time zones or when clocks move forward or backward. That is why I prefer to convert local time to UTC as early as possible.

The right approach

The main rule is simple: start with a timezone-aware local datetime, then convert it to UTC.

from datetime import datetime, timezone
from zoneinfo import ZoneInfo

local_time = datetime.now(ZoneInfo("America/New_York"))
utc_time = local_time.astimezone(timezone.utc)

print("Local time:", local_time)
print("UTC time:", utc_time)

That gives me the same moment in two time zones.

Example output

Local time: 2026-05-22 05:38:52.515610-04:00
UTC time: 2026-05-22 09:38:52.515610+00:00

I executed the above example code and added the screenshot below.

Convert Local Time to UTC in Python

This is the cleanest way to handle conversion because Python knows both the source timezone and the target timezone.

Why I prefer zoneinfo

For modern Python, I prefer zoneinfo because it is built into the standard library and supports real IANA time zones like America/New_York and America/Los_Angeles.

That matters because U.S. time zones are not just fixed offsets. They also need to handle daylight saving time correctly. A simple fixed offset like -05:00 can be wrong at some times of the year, but ZoneInfo knows the real rules.

Convert a specific U.S. local time

Sometimes I already know the local time and timezone. In that case, I build the datetime directly with tzinfo.

from datetime import datetime, timezone
from zoneinfo import ZoneInfo

meeting_time_local = datetime(2026, 5, 17, 9, 30, tzinfo=ZoneInfo("America/Chicago"))
meeting_time_utc = meeting_time_local.astimezone(timezone.utc)

print("Local meeting time:", meeting_time_local)
print("UTC meeting time:", meeting_time_utc)

This is useful when I am working with meetings, appointments, reminders, or scheduled tasks.

Naive vs aware datetime

This is one of the most important parts of the topic.

  • A naive datetime has no timezone information.
  • An aware datetime includes a timezone.
from datetime import datetime, timezone

naive_time = datetime.now()
aware_time = datetime.now(timezone.utc)

print("Naive:", naive_time)
print("Aware:", aware_time)

I try to avoid converting naive datetimes unless I know exactly which timezone they represent. If I guess wrong, the UTC conversion will be wrong too.

Convert a naive time safely

If I have a naive datetime but I know it represents a specific local zone, I attach the timezone first, then convert.

from datetime import datetime, timezone
from zoneinfo import ZoneInfo

naive_time = datetime(2026, 5, 17, 9, 30)
local_time = naive_time.replace(tzinfo=ZoneInfo("America/New_York"))
utc_time = local_time.astimezone(timezone.utc)

print("Local time:", local_time)
print("UTC time:", utc_time)

I executed the above example code and added the screenshot below.

How to Convert Local Time to UTC in Python

That is the right move when the original value came from a form, file, or database field and I know the intended timezone.

Convert user input to UTC

A very common real-world case is user input. Let’s say someone in Los Angeles enters a time in a form.

from datetime import datetime, timezone
from zoneinfo import ZoneInfo

user_input = "2026-05-17 08:15"
local_dt = datetime.strptime(user_input, "%Y-%m-%d %H:%M")
local_dt = local_dt.replace(tzinfo=ZoneInfo("America/Los_Angeles"))
utc_dt = local_dt.astimezone(timezone.utc)

print("User time:", local_dt)
print("UTC time:", utc_dt)

This pattern is useful for booking apps, reminders, calendar tools, and any form that accepts a local time from a user.

Store UTC, display local

This is the rule I follow in most projects:

  • Store timestamps in UTC.
  • Display them in the user’s local timezone.

That gives me clean storage and friendly output. If I save everything in UTC, I can later convert it to New York time, Chicago time, or Los Angeles time depending on who is viewing it.

from datetime import datetime, timezone
from zoneinfo import ZoneInfo

stored_utc = datetime.now(timezone.utc)
display_time = stored_utc.astimezone(ZoneInfo("America/New_York"))

print("Stored UTC:", stored_utc)
print("Displayed local:", display_time)

Format the UTC output

Raw datetime output is fine for debugging, but in reports or logs I usually format it.

from datetime import datetime, timezone
from zoneinfo import ZoneInfo

local_time = datetime.now(ZoneInfo("America/New_York"))
utc_time = local_time.astimezone(timezone.utc)

formatted = utc_time.strftime("%Y-%m-%d %H:%M:%S UTC")
print(formatted)

That creates a clear output like this:

2026-05-22 09:40:55 UTC

I executed the above example code and added the screenshot below.

Convert Local Time to UTC Python

Convert local time to UTC string

If I need a string directly, I can combine conversion and formatting.

from datetime import datetime, timezone
from zoneinfo import ZoneInfo

local_time = datetime.now(ZoneInfo("America/Los_Angeles"))
utc_string = local_time.astimezone(timezone.utc).strftime("%Y-%m-%d %H:%M:%S UTC")

print(utc_string)

This is handy when I want a readable value for audit logs, emails, or exported data.

Common mistakes

These are the mistakes I try to avoid:

  • Converting a naive datetime without knowing its original timezone.
  • Hardcoding offsets instead of using real time zone names.
  • Storing local time when UTC would be safer.
  • Mixing naive and aware datetime objects in the same workflow.
  • Forgetting that daylight saving time changes offsets.

If I avoid those problems, my datetime code becomes much easier to trust.

Local time examples

Here are a few U.S. examples I use often:

from datetime import datetime, timezone
from zoneinfo import ZoneInfo

new_york = datetime.now(ZoneInfo("America/New_York"))
chicago = datetime.now(ZoneInfo("America/Chicago"))
los_angeles = datetime.now(ZoneInfo("America/Los_Angeles"))

print("New York UTC:", new_york.astimezone(timezone.utc))
print("Chicago UTC:", chicago.astimezone(timezone.utc))
print("Los Angeles UTC:", los_angeles.astimezone(timezone.utc))

This is useful when I am testing applications for users in different U.S. regions.

When I use this in real projects

I use local-to-UTC conversion in all kinds of projects:

  • Scheduling systems.
  • Appointment booking apps.
  • Logging and monitoring tools.
  • API integrations.
  • Database timestamp fields.
  • Background jobs.
  • Calendar and reminder features.

In each of these cases, UTC gives me a stable internal timeline.

Quick comparison

Here is the simple choice I make depending on the situation:

SituationWhat I use
I already have a timezone-aware local timeastimezone(timezone.utc)
I have a naive time but know the timezonereplace(tzinfo=ZoneInfo(…)) then convert
I am collecting user-entered local timeParse it, attach the user’s timezone, then convert
I want to store reliable timestampsSave UTC

This table makes the decision easy when I’m writing production code.

Full example

Here is a complete example that shows the whole flow from local time to UTC:

from datetime import datetime, timezone
from zoneinfo import ZoneInfo

def convert_local_to_utc(year, month, day, hour, minute, tz_name):
local_dt = datetime(year, month, day, hour, minute, tzinfo=ZoneInfo(tz_name))
utc_dt = local_dt.astimezone(timezone.utc)
return local_dt, utc_dt

local_dt, utc_dt = convert_local_to_utc(2026, 5, 17, 9, 0, "America/New_York")

print("Local datetime:", local_dt)
print("UTC datetime:", utc_dt)
print("UTC formatted:", utc_dt.strftime("%Y-%m-%d %H:%M:%S UTC"))

This is a simple pattern I can reuse in apps, scripts, and tutorials.

FAQ

Can I use datetime.utcnow()?

I avoid it in new code. It gives a naive datetime, which is easier to mis-handle than a timezone-aware UTC value.

Should I use pytz or zoneinfo?

For new Python projects, I prefer zoneinfo. It is part of the standard library and handles real timezone names cleanly.

Why does daylight saving matter?

Because local offset can change during the year. A real timezone like America/New_York handles that correctly, while a fixed offset does not.

What is the safest format for storage?

I store UTC as a timezone-aware datetime or ISO 8601 string with +00:00.

Final thoughts

If I want to convert local time to UTC in Python, the safest approach is to start with a timezone-aware local datetime and use astimezone(timezone.utc). I store UTC, display local time only when needed, and always use real timezone names instead of guessing offsets.

You may also read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.