I prefer using the dateutil library for timezone handling and generally solid date parsing. If you were to get an ISO 8601 string like: 2010-05-08T23:41:54.000Z you'd have a fun time parsing that with strptime, especially if you didn't know up front whether or not the timezone was included. pyiso8601 has a couple of issues (check their tracker) that I ran into during my usage and it hasn't been updated in a few years. dateutil, by contrast, has been active and worked for me:

from dateutil import parser
yourdate = parser.parse(datestring)
๐ŸŒ
Note.nkmk.me
note.nkmk.me โ€บ home โ€บ python
Convert Between Isoformat String and datetime in Python | note.nkmk.me
August 22, 2023 - Handle date and time with the datetime module in Python ยท To convert date and time objects to ISO format (ISO 8601) strings, use the isoformat() method on date, time, and datetime objects.
Discussions

PSA: As of Python 3.11, `datetime.fromisoformat` supports most ISO 8601 formats (notably the "Z" suffix)
Fucking finally. More on reddit.com
๐ŸŒ r/Python
34
290
August 28, 2023
datetime - Python - Convert string representation of date to ISO 8601 - Stack Overflow
In Python, how can I convert a string like this: Thu, 16 Dec 2010 12:14:05 +0000 to ISO 8601 format, while keeping the timezone? Please note that the orginal date is string, and the output shoul... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Parse "Z" timezone suffix in datetime - Ideas - Discussions on Python.org
This is already opened as BPO 35829 but I wanted to ask about it over here for discussion. Problem Statement The function datetime.fromisoformat() parses a datetime in ISO-8601, format: >>> datetime.fromisoformat('2019-08-28T14:34:25.518993+00:00') datetime.datetime(2019, 8, 28, 14, 34, 25, ... More on discuss.python.org
๐ŸŒ discuss.python.org
10
August 28, 2019
Convert integer to iso date
Look at strptime from the datetime library. https://docs.python.org/3/library/datetime.html#datetime.datetime.strptime More on reddit.com
๐ŸŒ r/learnpython
2
0
January 10, 2022
Top answer
1 of 14
1247

Local to ISO 8601:

import datetime
datetime.datetime.now().isoformat()
>>> '2024-08-01T14:38:32.499588'

UTC to ISO 8601:

import datetime
datetime.datetime.now(datetime.timezone.utc).isoformat()
>>> '2024-08-01T04:38:47.731215+00:00'

Local to ISO 8601 without microsecond:

import datetime
datetime.datetime.now().replace(microsecond=0).isoformat()
>>> '2024-08-01T14:38:57'

UTC to ISO 8601 with timezone information (Python 3):

import datetime
datetime.datetime.now(datetime.timezone.utc).isoformat()
>>> '2024-08-01T04:39:06.274874+00:00'

Local to ISO 8601 with timezone information (Python 3):

import datetime
datetime.datetime.now().astimezone().isoformat()
>>> '2024-08-01T14:39:16.698776+10:00'

Local to ISO 8601 with local timezone information without microsecond (Python 3):

import datetime
datetime.datetime.now().astimezone().replace(microsecond=0).isoformat()
>>> '2024-08-01T14:39:28+10:00'

Notice there is a bug when using astimezone() on utcnow(). This gives an incorrect result:

datetime.datetime.utcnow().astimezone().isoformat() #Incorrect result, do not use.

.utcnow() is deprecated, use .now(datetime.timezome.utc) instead.

For Python 2, see and use pytz.

2 of 14
139

ISO 8601 allows a compact representation with no separators except for the T, so I like to use this one-liner to get a quick timestamp string:

>>> datetime.datetime.now(datetime.UTC).strftime("%Y%m%dT%H%M%S.%fZ")
'20180905T140903.591680Z'

If you don't need the microseconds, just leave out the .%f part:

>>> datetime.datetime.now(datetime.UTC).strftime("%Y%m%dT%H%M%SZ")
'20180905T140903Z'

For local time:

>>> datetime.datetime.now(datetime.timezone(datetime.timedelta(hours=-5))).strftime("%Y-%m-%dT%H:%M:%S%:z")
'2018-09-05T14:09:03-05:00'

In general, I recommend you leave the punctuation in. RFC 3339 recommends that style because if everyone uses punctuation, there isn't a risk of things like multiple ISO 8601 strings being sorted in groups on their punctuation. So the one liner for a compliant string would be:

>>> datetime.datetime.now(datetime.UTC).strftime("%Y-%m-%dT%H:%M:%SZ")
'2018-09-05T14:09:03Z'
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ library โ€บ datetime.html
datetime โ€” Basic date and time types
Return a time corresponding to a time_string in any valid ISO 8601 format, with the following exceptions: Time zone offsets may have fractional seconds. The leading T, normally required in cases where there may be ambiguity between a date and a time, is not required. Fractional seconds may have any number of digits (anything beyond 6 will be truncated). Fractional hours and minutes are not supported. ... >>> import datetime as dt >>> dt.time.fromisoformat('04:23:01') datetime.time(4, 23, 1) >>> dt.time.fromisoformat('T04:23:01') datetime.time(4, 23, 1) >>> dt.time.fromisoformat('T042301') date
๐ŸŒ
Reddit
reddit.com โ€บ r/python โ€บ psa: as of python 3.11, `datetime.fromisoformat` supports most iso 8601 formats (notably the "z" suffix)
r/Python on Reddit: PSA: As of Python 3.11, `datetime.fromisoformat` supports most ISO 8601 formats (notably the "Z" suffix)
August 28, 2023 -

In Python 3.10 and earlier, datetime.fromisoformat only supported formats outputted by datetime.isoformat. This meant that many valid ISO 8601 strings could not be parsed, including the very common "Z" suffix (e.g. 2000-01-01T00:00:00Z).

I discovered today that 3.11 supports most ISO 8601 formats. I'm thrilled: I'll no longer have to use a third-party library to ingest ISO 8601 and RFC 3339 datetimes. This was one of my biggest gripes with Python's stdlib.

It's not 100% standards compliant, but I think the exceptions are pretty reasonable:

  • Time zone offsets may have fractional seconds.

  • The T separator may be replaced by any single unicode character.

  • Ordinal dates are not currently supported.

  • Fractional hours and minutes are not supported.

https://docs.python.org/3/library/datetime.html#datetime.datetime.fromisoformat

๐ŸŒ
LabEx
labex.io โ€บ tutorials โ€บ python-how-to-create-datetime-objects-from-iso-8601-date-strings-417942
How to create datetime objects from ISO-8601 date strings | LabEx
from datetime import datetime iso_date_string = "2023-04-15T12:34:56Z" datetime_obj = datetime.fromisoformat(iso_date_string) print(datetime_obj) ## Output: 2023-04-15 12:34:56 ยท In this example, we first import the datetime module from the Python standard library.
๐ŸŒ
Statology
statology.org โ€บ home โ€บ how to work with date and time in iso format in python
How to Work with Date and Time in ISO Format in Python
August 16, 2024 - This example demonstrates the conversion between timezone-aware datetime objects and ISO formatted strings. The output shows that iso_with_tz is a string representation, while parsed_dt is a datetime object.
Find elsewhere
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ isoformat-method-of-datetime-class-in-python
Isoformat() Method Of Datetime Class In Python - GeeksforGeeks
October 15, 2021 - # Python3 code to demonstrate # Getting date and time values # in ISO 8601 format # importing datetime module import datetime # Getting today's date and time DateTime_in_ISOFormat = datetime.datetime.now() # Printing Today's date and time in ISO format of # auto value for the format specifier print(DateTime_in_ISOFormat.isoformat("#", "auto")) # Printing Today's date and time format specifier # as hours print(DateTime_in_ISOFormat.isoformat("#", "hours")) # Printing Today's date and time format specifier # as minutes print(DateTime_in_ISOFormat.isoformat("#", "minutes")) # Printing Today's dat
๐ŸŒ
Personmeetup
personmeetup.ca โ€บ blog โ€บ getting-iso-time-with-python-datetime
Getting ISO Time with Python Datetime - Person Meetup
From that submodule, only two functions will be used: .now() to get the current time according to the system clock and .isoformat() to convert the datetime.datetime object into a string that's in ISO 8601 format.
๐ŸŒ
PYnative
pynative.com โ€บ home โ€บ python โ€บ python datetime โ€บ python iso 8601 datetime
Python Get ISO 8601 Datetime [4 Ways] โ€“ PYnative
May 27, 2022 - ... Next, Add default timezone information to datetime using the astimezone() function. The local timezone or default is your systemโ€™s timezone information. In the end, use the isoformat() method to get the current isoformat datetime string including the default timezone.
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ what-is-dateisoformat-in-python
What is date.isoformat() in Python?
This isoformat() function belongs to the datetime module in Python. It converts the Date object value into a string in ISO format. It follows the ISO 8601, or YYYY-MM-DD, format.
๐ŸŒ
LabEx
labex.io โ€บ tutorials โ€บ date-to-iso-format-13733
Convert Date to ISO Format in Python | LabEx
In this challenge, you will write a function that converts a date to its ISO-8601 representation. Write a function to_iso_date(d) that takes a datetime.datetime object as its argument and returns a string ...
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ article โ€บ how-do-i-get-an-iso-8601-date-in-string-format-in-python
How do I get an ISO 8601 date in string format in Python?
June 16, 2025 - ... T: It is a character used to separate date and time fields. It is an optional parameter having a default value of "T". ... The isoformat() method returns a string format of the current datetime.date object in ISO 8601 format.
๐ŸŒ
Deepan Seeralan
deepanseeralan.com โ€บ tech โ€บ iso-datetime-conversions-python
Converting ISO 8601 formatted date into datetime objects in Python - Deepan Seeralan
January 9, 2020 - I wanted to parse this date string at the skill endpoint and respond to the user accordingly. Since the endpoint was hosted as python funtion in AWS Lamda, I looked into my go to python reference, the docs page. If the date format is YYYY-MM-DD, Pythonโ€™s datetime.datetime has some nifty helpers to convert the date string into a datetime object.
๐ŸŒ
Python.org
discuss.python.org โ€บ ideas
Parse "Z" timezone suffix in datetime - Ideas - Discussions on Python.org
August 28, 2019 - Problem Statement The function datetime.fromisoformat() parses a datetime in ISO-8601, format: >>> datetime.fromisoformat('2019-08-28T14:34:25.518993+00:00') datetime.datetime(2019, 8, 28, 14, 34, 25, ...
๐ŸŒ
Learn By Example
learnbyexample.org โ€บ working-with-iso-8601-in-python
Working with ISO 8601 in Python - Learn By Example
April 23, 2024 - And then use the isoformat() method. from datetime import datetime # Example datetime with microseconds dt = datetime(2023, 4, 5, 12, 30, 45, 123456) # Format the datetime object into an ISO 8601 string iso_formatted_dt = dt.isoformat() ...
๐ŸŒ
DataCamp
campus.datacamp.com โ€บ courses โ€บ working-with-dates-and-times-in-python โ€บ combining-dates-and-times
Recreating ISO format with strftime() | Python
Complete fmt to match the format of ISO 8601. Print first_start with both .isoformat() and .strftime(); they should match. Have a go at this exercise by completing this sample code. # Import datetime from datetime import datetime # Pull out the start of the first trip first_start = ...
๐ŸŒ
Pythontic
pythontic.com โ€บ datetime โ€บ datetime โ€บ isoformat
The isoformat() method of datetime class in Python | Pythontic.com
In Python the isoformat() method of datetime class returns the date-time string with the specified separator. If no separator is specified space is printed