Is the UTC offset format in your string +09 or +0900 ?

If the offset in your string is 0900 you can use the below .If your UTC offset is only +09 as you mentioned in your question , you can pad the string with 00 and get the below code to work .

Code:

import datetime  
time="2016-02-18 16:13:07+0900"  
new_time=datetime.datetime.strptime(time,"%Y-%m-%d %H:%M:%S%z")  
print(new_time)  
new_time_python=datetime.datetime.strftime(new_time,"%m-%d-%y")  
print(new_time_python)  

Output

2016-02-18 16:13:07+09:00  
02-18-16 
Answer from Rahul.M on Stack Overflow
🌐
Python documentation
docs.python.org › 3 › library › datetime.html
datetime — Basic date and time types
If your application uses this convention and your system time zone is not set to UTC, you can obtain the POSIX timestamp by supplying tzinfo=timezone.utc:
🌐
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.
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
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.to_datetime.html
pandas.to_datetime — pandas 3.0.1 documentation - PyData |
If Timestamp convertible (Timestamp, dt.datetime, np.datetimt64 or date string), origin is set to Timestamp identified by origin. If a float or integer, origin is the difference (in units determined by the unit argument) relative to 1970-01-01. ... If True, use a cache of unique, converted dates to apply the datetime conversion. May produce significant speed-up when parsing duplicate date strings, especially ones with timezone offsets.
🌐
DataCamp
datacamp.com › tutorial › converting-strings-datetime-objects
Convert String to DateTime in Python: Complete Guide with Examples | DataCamp
June 8, 2018 - Learn how to convert strings to datetime objects in Python using strptime(), dateutil, and pandas. Includes code examples, timezone handling, and troubleshooting tips.
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.Timestamp.html
pandas.Timestamp — pandas 3.0.1 documentation
Python datetime.datetime object. ... There are essentially three calling conventions for the constructor. The primary form accepts four parameters. They can be passed by position or keyword. The other two forms mimic the parameters from datetime.datetime. They can be passed by either position or keyword, but not both mixed together. ... >>> pd.Timestamp(1513393355, unit='s', tz='US/Pacific') Timestamp('2017-12-15 19:02:35-0800', tz='US/Pacific')
🌐
GeeksforGeeks
geeksforgeeks.org › convert-string-to-datetime-in-python-with-timezone
Convert string to datetime in Python with timezone - GeeksforGeeks
November 29, 2022 - In this example, microseconds and time zone parts are removed from first example, so we need to remove microseconds and time zone abbreviations also from format string ... # Python3 code to demonstrate # Getting datetime object using a date_string # importing datetime module import datetime # datestring for which datetime_obj required date_string = 'Sep 01 2021 03:27:05 PM' # using strptime() to get datetime object datetime_obj = datetime.datetime.strptime(date_string, '%b %d %Y %I:%M:%S %p') # Printing datetime print(datetime_obj)
Find elsewhere
🌐
PYnative
pynative.com › home › python › python datetime › python string to datetime using strptime()
Python String to DateTime using Strptime() [5 Ways] – PYnative
December 5, 2021 - from datetime import datetime # String to Date birthday = "23/02/2012 09::30::23" # String to timestamp timeStamp = datetime.strptime(birthday, "%d/%m/%Y %H::%M::%S").timestamp() print("TimeStamp::", timeStamp) # Output 1329969623.0Code language: Python (python) Run · While the datetime module’s strptime() handles most of the date formats, there are few drawbacks, as mentioned below. We need to import many modules like datetime, time, calendar, pytz, and more to handle some complex date formats. Handling naive and aware timezones are complicated.
🌐
Rip Tutorial
riptutorial.com › parsing a string into a timezone aware datetime object
Python Language Tutorial => Parsing a string into a timezone aware...
Data Visualization with Python · Database Access · Date and Time · Basic datetime objects usage · Computing time differences · Constructing timezone-aware datetimes · Converting timestamp to datetime · Fuzzy datetime parsing (extracting datetime out of a text) Get an ISO 8601 timestamp · Iterate over dates · Parsing a string into a timezone aware datetime object ·
🌐
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 ...
🌐
Janakiev
janakiev.com › blog › time-and-timezones-in-python
Working with Time and Time Zones in Python - njanakiev
June 7, 2017 - You know that the time zone is Europe/Paris or Central European Time (CET) (UTC+01:00) and you want to normalize the timestamp to UTC. This can be done by using the pyhton datetime object as follows · import datetime import pytz timestring = "2017-05-30T23:51:03Z" # Create datetime object d = datetime.datetime.strptime(timestring, "%Y-%m-%dT%H:%M:%SZ") print(d.tzinfo) # Return time zone info print(d.strftime("%d.%m.%y %H:%M:%S")) # Set the time zone to 'Europe/Paris' d = pytz.timezone('Europe/Paris').localize(d) print(d.tzinfo) # Return time zone info # Transform the time to UTC d = d.astimezone(pytz.utc) print(d.tzinfo) # Return time zone info print(d.strftime("%d.%m.%y %H:%M:%S"))
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-string-to-datetime-strptime
How To Convert a String to a datetime Object in Python | DigitalOcean
December 13, 2024 - In this article, you’ll use strptime() to convert strings into datetime and struct_time() objects. Deploy your Python applications from GitHub using DigitalOcean App Platform.
🌐
Medium
medium.com › @lawjimmy123 › learn-how-to-handle-timestamps-and-timezones-in-python-in-2-minutes-50d0d3e8fa3a
Learn how to handle timestamps and timezones in Python in 2 minutes | by Jimmy Law | Medium
January 16, 2022 - If you are working with well designed APIs or databases, you will likely find dates / times data stored as timestamps. However in your applications or reports, dates and times are generally presented as human readable strings, e.g. “2021–11–23 11:00”. We can easily move between timestamps and human readable strings via Python’s built-in datetime package, for example here is how to convert human readable strings into Unix timestamps.
🌐
GeeksforGeeks
geeksforgeeks.org › convert-date-string-to-timestamp-in-python
Convert date string to timestamp in Python - GeeksforGeeks
August 28, 2023 - date and time in form of a string before storing it into a database, we convert that date and time string into a timestamp. Python provides various ways of converting the date to timestamp.
🌐
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 - The above references and general use of the strptime method provides the groundwork for converting strings to datetime objects in Python. The essential steps of this process are as follows: ... Below is a slightly more complex example in which ...
🌐
Python
docs.python.org › 3.3 › library › datetime.html
8.1. datetime — Basic date and time types — Python 3.3.7 documentation
May 11, 2020 - If your application uses this convention and your system timezone is not set to UTC, you can obtain the POSIX timestamp by supplying tzinfo=timezone.utc:
🌐
Flexiple
flexiple.com › python › python-unix-timestamp
Converting DateTime to UNIX Timestamp in Python - Flexiple
Datetime to Unix TimestampDatetime to Unix Timestamp in UTC TimezoneDatetime.date to Unix TimestampDateTime String to Unix Timestamp ... Converting DateTime to UNIX Timestamp in Python transforms a standard date and time format into a UNIX timestamp, which is the number of seconds that have elapsed in current time since January 1, 1970 (UTC).