As of Python 3.7, datetime.datetime.fromisoformat() can handle your format:

>>> import datetime
>>> datetime.datetime.fromisoformat('2012-11-01T04:16:13-04:00')
datetime.datetime(2012, 11, 1, 4, 16, 13, tzinfo=datetime.timezone(datetime.timedelta(days=-1, seconds=72000)))

In older Python versions you can't, not without a whole lot of painstaking manual timezone defining.

Python versions before version 3.9 do not include a timezone database, because it would be outdated too quickly. Instead, for those versions Python relied on external libraries, which can have a far faster release cycle, to provide properly configured timezones for you.

As a side-effect, this means that timezone parsing also needs to be an external library. If dateutil is too heavy-weight for you, use iso8601 instead, it'll parse your specific format just fine:

>>> import iso8601
>>> iso8601.parse_date('2012-11-01T04:16:13-04:00')
datetime.datetime(2012, 11, 1, 4, 16, 13, tzinfo=<FixedOffset '-04:00'>)

iso8601 is a whopping 4KB small. Compare that tot python-dateutil's 148KB.

As of Python 3.2 Python can handle simple offset-based timezones, and %z will parse -hhmm and +hhmm timezone offsets in a timestamp. That means that for a ISO 8601 timestamp you'd have to remove the : in the timezone:

>>> from datetime import datetime
>>> iso_ts = '2012-11-01T04:16:13-04:00'
>>> datetime.strptime(''.join(iso_ts.rsplit(':', 1)), '%Y-%m-%dT%H:%M:%S%z')
datetime.datetime(2012, 11, 1, 4, 16, 13, tzinfo=datetime.timezone(datetime.timedelta(-1, 72000)))

The lack of proper ISO 8601 parsing was being tracked in Python issue 15873 (since migrated to GitHub issue #60077).

Answer from Martijn Pieters on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › convert-string-to-datetime-in-python-with-timezone
Convert string to datetime in Python with timezone - GeeksforGeeks
July 23, 2025 - Explanation: arrow.get(s) parses the date-time string s into an Arrow datetime object with timezone support, without requiring an explicit format string, providing a clean and user-friendly API.
Top answer
1 of 7
167

As of Python 3.7, datetime.datetime.fromisoformat() can handle your format:

>>> import datetime
>>> datetime.datetime.fromisoformat('2012-11-01T04:16:13-04:00')
datetime.datetime(2012, 11, 1, 4, 16, 13, tzinfo=datetime.timezone(datetime.timedelta(days=-1, seconds=72000)))

In older Python versions you can't, not without a whole lot of painstaking manual timezone defining.

Python versions before version 3.9 do not include a timezone database, because it would be outdated too quickly. Instead, for those versions Python relied on external libraries, which can have a far faster release cycle, to provide properly configured timezones for you.

As a side-effect, this means that timezone parsing also needs to be an external library. If dateutil is too heavy-weight for you, use iso8601 instead, it'll parse your specific format just fine:

>>> import iso8601
>>> iso8601.parse_date('2012-11-01T04:16:13-04:00')
datetime.datetime(2012, 11, 1, 4, 16, 13, tzinfo=<FixedOffset '-04:00'>)

iso8601 is a whopping 4KB small. Compare that tot python-dateutil's 148KB.

As of Python 3.2 Python can handle simple offset-based timezones, and %z will parse -hhmm and +hhmm timezone offsets in a timestamp. That means that for a ISO 8601 timestamp you'd have to remove the : in the timezone:

>>> from datetime import datetime
>>> iso_ts = '2012-11-01T04:16:13-04:00'
>>> datetime.strptime(''.join(iso_ts.rsplit(':', 1)), '%Y-%m-%dT%H:%M:%S%z')
datetime.datetime(2012, 11, 1, 4, 16, 13, tzinfo=datetime.timezone(datetime.timedelta(-1, 72000)))

The lack of proper ISO 8601 parsing was being tracked in Python issue 15873 (since migrated to GitHub issue #60077).

2 of 7
75

Here is the Python Doc for datetime object using dateutil package..

from dateutil.parser import parse

get_date_obj = parse("2012-11-01T04:16:13-04:00")
print get_date_obj
Discussions

datetime - Python Timezone conversion - Stack Overflow
-1 How to convert a list of timestamps in an excel file from UTC to San Francisco time (Pacific Time) with python? 0 How to convert string variable which is in UTC to AEST('Australia/Melbourne') timezone using python code More on stackoverflow.com
🌐 stackoverflow.com
django - convert python datetime with timezone to string - Stack Overflow
I have date time tuples in the format of datetime.datetime(2010, 7, 1, 0, 0, tzinfo= ) How can I convert that into a date time string such as 2008-11-10 17:53:59 I am really just gettin... More on stackoverflow.com
🌐 stackoverflow.com
How to convert a string date and time to a datetime with Eastern timezone?
It sounds like you have run into a common issue. 'US/Eastern' time is not the same thing as 'Eastern Standard Time'. It's there for backwards compatibility reasons. The timezone you might be looking for is called "America/New_York" More on reddit.com
🌐 r/learnpython
6
1
October 23, 2020
How to deal with datetime timezones?
Obligatory video of Tom Scott about timezones . More on reddit.com
🌐 r/learnpython
7
4
June 29, 2022
🌐
DataCamp
datacamp.com › tutorial › converting-strings-datetime-objects
Convert String to DateTime in Python: Complete Guide with Examples | DataCamp
June 8, 2018 - Yes, you can handle time zones by using the pytz library in combination with datetime. First, convert the string to a datetime object, and then attach a time zone using pytz.timezone():
Top answer
1 of 10
148

I have found that the best approach is to convert the "moment" of interest to a utc-timezone-aware datetime object (in python, the timezone component is not required for datetime objects).

Then you can use astimezone to convert to the timezone of interest (reference).

from datetime import datetime
import pytz

utcmoment_naive = datetime.utcnow()
utcmoment = utcmoment_naive.replace(tzinfo=pytz.utc)

# print "utcmoment_naive: {0}".format(utcmoment_naive) # python 2
print("utcmoment_naive: {0}".format(utcmoment_naive))
print("utcmoment:       {0}".format(utcmoment))

localFormat = "%Y-%m-%d %H:%M:%S"

timezones = ['America/Los_Angeles', 'Europe/Madrid', 'America/Puerto_Rico']

for tz in timezones:
    localDatetime = utcmoment.astimezone(pytz.timezone(tz))
    print(localDatetime.strftime(localFormat))

# utcmoment_naive: 2017-05-11 17:43:30.802644
# utcmoment:       2017-05-11 17:43:30.802644+00:00
# 2017-05-11 10:43:30
# 2017-05-11 19:43:30
# 2017-05-11 13:43:30

So, with the moment of interest in the local timezone (a time that exists), you convert it to utc like this (reference).

localmoment_naive = datetime.strptime('2013-09-06 14:05:10', localFormat)

localtimezone = pytz.timezone('Australia/Adelaide')

try:
    localmoment = localtimezone.localize(localmoment_naive, is_dst=None)
    print("Time exists")

    utcmoment = localmoment.astimezone(pytz.utc)

except pytz.exceptions.NonExistentTimeError as e:
    print("NonExistentTimeError")
2 of 10
75

Python 3.9 adds the zoneinfo module so now only the the standard library is needed!

>>> from zoneinfo import ZoneInfo
>>> from datetime import datetime

>>> d = datetime(2020, 10, 31, 12, tzinfo=ZoneInfo('America/Los_Angeles'))
>>> d.astimezone(ZoneInfo('Europe/Berlin'))  # 12:00 in Cali will be 20:00 in Berlin
datetime.datetime(2020, 10, 31, 20, 0, tzinfo=zoneinfo.ZoneInfo(key='Europe/Berlin'))

Wikipedia list of available time zones


Some functions such as now() and utcnow() return timezone-unaware datetimes, meaning they contain no timezone information. I recommend only requesting timezone-aware values from them using the keyword tz=ZoneInfo('localtime').

If astimezone gets a timezone-unaware input, it will assume it is local time, which can lead to errors:

>>> datetime.utcnow()  # UTC -- NOT timezone-aware!!
datetime.datetime(2020, 6, 1, 22, 39, 57, 376479)
>>> datetime.now()     # Local time -- NOT timezone-aware!!
datetime.datetime(2020, 6, 2, 0, 39, 57, 376675)

>>> datetime.now(tz=ZoneInfo('localtime'))  # timezone-aware
datetime.datetime(2020, 6, 2, 0, 39, 57, 376806, tzinfo=zoneinfo.ZoneInfo(key='localtime'))
>>> datetime.now(tz=ZoneInfo('Europe/Berlin'))  # timezone-aware
datetime.datetime(2020, 6, 2, 0, 39, 57, 376937, tzinfo=zoneinfo.ZoneInfo(key='Europe/Berlin'))
>>> datetime.utcnow().astimezone(ZoneInfo('Europe/Berlin'))  # WRONG!!
datetime.datetime(2020, 6, 1, 22, 39, 57, 377562, tzinfo=zoneinfo.ZoneInfo(key='Europe/Berlin'))

Windows has no system time zone database, so here an extra package is needed:

pip install tzdata  

There is a backport to allow use in Python 3.6 to 3.8:

sudo pip install backports.zoneinfo

Then:

from backports.zoneinfo import ZoneInfo
🌐
Vultr
docs.vultr.com › python › examples › convert-string-to-datetime
Python Program to Convert String to Datetime | Vultr Docs
November 22, 2024 - Use Python's pytz module to handle time zone conversions. Convert a string with a time zone specification to a timezone-aware datetime object.
🌐
Python Guides
pythonguides.com › convert-a-string-to-datetime-in-python
Convert Python String to Datetime with Timezone
September 23, 2025 - Learn step-by-step how to convert Python string to datetime with timezone using datetime, pytz, and dateutil. Includes full code examples and practical tips.
Find elsewhere
🌐
Python documentation
docs.python.org › 3 › library › datetime.html
datetime — Basic date and time types
If called without arguments (or with tz=None) the system local time zone is assumed for the target time zone. The .tzinfo attribute of the converted datetime instance will be set to an instance of timezone with the zone name and offset obtained from the OS.
🌐
Stack Abuse
stackabuse.com › converting-strings-to-datetime-in-python
Converting Strings to datetime in Python
June 21, 2023 - Then using the astimezone() method, we have converted this datetime to "Europe/London" timezone. Both datetimes will print different values, using UTC offset as a reference link between them: America/New_York: 2022-11-30 21:24:30.123400-05:00 Europe/London: 2022-12-01 02:24:30.123400+00:00 ...
🌐
CodingTechRoom
codingtechroom.com › question › how-to-convert-string-to-time-change-timezone-back-to-string
How to Convert a String to Time, Change the Timezone, and Convert Back to String - CodingTechRoom
Convert to the desired timezone: Change the timezone of your localized `datetime` object using the `astimezone()` method. Convert back to string: Finally, format the updated `datetime` object back to a string using `strftime` for user-friendly display.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-timezone-conversion
Python | Timezone Conversion - GeeksforGeeks
December 19, 2022 - Most datetime items came back from the dateutil parser are naive, which means they don't have an explicit tzinfo. tzinfo determines the timezone and UTC offset. It is the standard ISO format for UTC datetime strings. UTC is the coordinated universal time, and is fundamentally the equivalent as GMT. ISO is the International Standards Organization, which in addition to other things, determines standard datetime designing. Python datetime items can either be naive or mindful.
🌐
alpharithms
alpharithms.com › home › tutorials › converting between strings and datetime objects in python
Converting Between Strings and Datetime Objects in Python - αlphαrithms
June 17, 2022 - Finally, we use the strptime method to convert the now-aware date object into a final output string which reflects the timezone information via the %T%t format syntax. The result of this code is as follows: Initial Timezone: None Updated Timezone: UTC Converted Timezone: 2022-06-17T18:23:59 UTC+0000 · The datetime module in Python allows developers to easily convert between strings and datetime objects.
🌐
TutorialsPoint
tutorialspoint.com › How-to-convert-date-and-time-with-different-timezones-in-Python
How to convert date and time with different timezones in Python?
August 25, 2023 - Get the current time using the datetime.now() function and pass the above second?time zone i.e 'US/Eastern' time zone as an argument to it(Here it converts the current date and time to the 'US/Eastern' timezone). Use the strftime() function to format the above datetime object and print it. ...
🌐
Analytics Vidhya
analyticsvidhya.com › home › convert string to datetime and vice-versa in python
Convert String to DateTime and Vice-Versa in Python - Analytics Vidhya
February 6, 2024 - If you’re working with pandas, the to_datetime() method can convert a string to a DateTime object. It provides additional functionalities, such as handling missing values and timezones.
🌐
Techmonger
techmonger.github.io › 32 › pytz-example-conversion
Simple PyTz Tutorial - Convert Date Time Across Time Zones - Tech Monger
August 22, 2023 - target_date_with_timezone.strftime('%Y-%m-%d %H:%M') To convert string formatted time to python datetime object use strptime of datetime module.
🌐
PYnative
pynative.com › home › python › python datetime › working with timezones in python
Python TimeZone: A Guide To Work With Different Timezones – PYnative
December 5, 2021 - US Central DateTime: 2021:07:08 22:30:06 CDT -0500 TimeZone Name: CDT UTC Offset -1 day, 19:00:00 DST: 1:00:00 · The datetime modules have the timezone class, which in turn is the subclass of the abstract base class tzinfo · Use the datetime.ast...
🌐
SaltyCrane
saltycrane.com › blog › 2009 › 05 › converting-time-zones-datetime-objects-python
Converting time zones for datetime objects in Python - SaltyCrane Blog
July 23, 2025 - Date: 2009-05-05 | Modified: 2014-05-28 | Tags: datetime, python | 12 Comments · I am using pytz, which is a time zone definitions package. You can install it using Easy Install. On Ubuntu, do this: ... from datetime import datetime from pytz import timezone date_str = "2009-05-05 22:28:15" datetime_obj = datetime.strptime(date_str, "%Y-%m-%d %H:%M:%S") datetime_obj_utc = datetime_obj.replace(tzinfo=timezone('UTC')) print datetime_obj_utc.strftime("%Y-%m-%d %H:%M:%S %Z%z")
🌐
Python
docs.python.org › 3 › library › time.html
time — Time access and conversions
If no DST timezone is defined, the second string should not be used. See note below. ... For the above Timezone constants (altzone, daylight, timezone, and tzname), the value is determined by the timezone rules in effect at module load time or the last time tzset() is called and may be incorrect for times in the past. It is recommended to use the tm_gmtoff and tm_zone results from localtime() to obtain timezone information.