For those looking to match datetime's ISO 8601-compliant isoformat:

from datetime import datetime

now = datetime.now()
isoformat = now.isoformat()
diy_isoformat = now.strftime("%Y-%m-%dT%H:%M:%S.%f")
assert isoformat == diy_isoformat
print(isoformat)

Will print:

2022-06-17T15:33:08.893344
Answer from Intrastellar Explorer on Stack Overflow
🌐
Python documentation
docs.python.org › 3 › library › datetime.html
datetime — Basic date and time types
For a date d, str(d) is equivalent to d.isoformat(). date.ctime()¶ · Return a string representing the date: >>> import datetime as dt >>> dt.date(2002, 12, 4).ctime() 'Wed Dec 4 00:00:00 2002' d.ctime() is equivalent to: time.ctime(time.mktime(d.timetuple())) on platforms where the native C ctime() function (which time.ctime() invokes, but which date.ctime() does not invoke) conforms to the C standard. date.strftime(format)¶ ·
Discussions

datetime - ISO time (ISO 8601) in Python - Stack Overflow
The correct way to output the time ...ime.now().strftime("%Y-%m-%dT%H:%M:%S%z")’. I’ve spent 2h trying to find a bug in my code… 2020-07-31T13:18:18.24Z+00:00 ... @Dr_Zaszuś That's not right. %z generates a timezone offset for your local time, which is not what I recommend for timestamps, especially if you're looking at multiple systems. A literal Z is ISO 8601 shorthand ... More on stackoverflow.com
🌐 stackoverflow.com
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
Add ISO Basic format support to datetime.isoformat() and date.isoformat()
Feature or enhancement Proposal: In additional to the popular ISO 8601 Extended format, there's also an ISO 8601 Basic format for datetimes which is useful for filenames and URL components as i... More on github.com
🌐 github.com
13
May 11, 2024
datetime - How do you convert a python time.struct_time object into a ISO string? - Stack Overflow
I have a Python object: time.struct_time(tm_year=2013, tm_mon=10, tm_mday=11, tm_hour=11, tm_min=57, tm_sec=12, tm_wday=4, tm_yday=284, tm_isdst=0) And I need to get an ISO string: '2013-10-11T11... More on stackoverflow.com
🌐 stackoverflow.com
🌐
DataCamp
campus.datacamp.com › courses › working-with-dates-and-times-in-python › combining-dates-and-times
Recreating ISO format with strftime() | Python
# Import datetime from datetime import datetime # Pull out the start of the first trip first_start = onebike_datetimes[0]['start'] # Format to feed to strftime() fmt = "____" # Print out date with .isoformat(), then with .strftime() to compare print(first_start.isoformat()) print(____)
Top answer
1 of 14
1248

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'
🌐
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.
🌐
Runebook.dev
runebook.dev › en › docs › python › library › datetime › datetime.datetime.isoformat
Troubleshooting Python's isoformat(): Timezones, Microseconds, and strftime()
import datetime dt = datetime.datetime.now() # Format similar to isoformat(timespec='seconds') iso_like_string = dt.strftime('%Y-%m-%dT%H:%M:%S') print(f"strftime for ISO-like: {iso_like_string}") # A completely custom format (e.g., for logging) custom_log_format = dt.strftime('The date is %B %d, %Y at %I:%M %p') print(f"Custom format: {custom_log_format}") ... Let's break down bdb. Breakpoint. bpbynumber, common issues, and alternative approaches.In Python's bdb (debugger base) module
🌐
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

Find elsewhere
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.Timestamp.isoformat.html
pandas.Timestamp.isoformat — pandas 3.0.1 documentation
Timestamp.strftime · Return a formatted string. Timestamp.isocalendar · Return a tuple containing ISO year, week number and weekday.
🌐
TutorialsPoint
tutorialspoint.com › 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 - from datetime import datetime ... belongs to the datetime module in Python. This method accepts a format string as a parameter and converts the current date object into a string as per the specified format, and returns the result....
🌐
Michael Currin
michaelcurrin.github.io › dev-cheatsheets › cheatsheets › python › time-handling.html
Date and time handling | Dev Cheatsheets
datetime module in Python. strftime cheatsheet · unixtimestamp.com converter. Unix time on Wikipedia. ISO 8061 standard on Wikipedia - date, time, and datetime. Date · 2022-01-08 · Date and time in UTC · 2022-01-08T08:18:20+00:00 · 2022-01-08T08:18:20Z ·
🌐
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
🌐
GeeksforGeeks
geeksforgeeks.org › python › isoformat-method-of-datetime-class-in-python
Isoformat() Method Of Datetime Class In Python - GeeksforGeeks
October 15, 2021 - Example 2: In the below example, the isoformat() function has been called on today's date and time and it returns the same today's date and time string in ISO 8601 format. ... # Python3 code to demonstrate # Getting date and time values # in ISO 8601 format # importing datetime and time module import datetime import time # Getting today's date and time todays_Date = datetime.datetime.now() # Calling the isoformat() function over the # today's date and time DateTime_in_ISOFormat = todays_Date.isoformat() # Printing Today's date and time in ISO format print("Today's date and time in ISO Format: %s" % DateTime_in_ISOFormat)
🌐
GitHub
github.com › python › cpython › issues › 118948
Add ISO Basic format support to datetime.isoformat() and date.isoformat() · Issue #118948 · python/cpython
May 11, 2024 - extension-modulesC modules in the Modules dirC modules in the Modules dirstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement ... In additional to the popular ISO 8601 Extended format, there's also an ISO 8601 Basic format for datetimes which is useful for filenames and URL components as it avoids characters such as eg.
Published   May 11, 2024
Author   mohd-akram
🌐
GitHub
gist.github.com › kkAyataka › bc12e99f850437b8480c
Python datetime to iso format string and iso format string to datetime · GitHub
Python datetime to iso format string and iso format string to datetime - datetime_to_string_to.py
🌐
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 - The ISO 8601 standard specifies several formats, but the most common ones are: ... Python’s datetime module provides built-in support for working with ISO formatted date and time strings.
🌐
LabEx
labex.io › tutorials › date-to-iso-format-13733
Convert Date to ISO Format in Python | LabEx
from datetime import datetime def to_iso_date(d): return d.isoformat()
🌐
PyPI
pypi.org › project › isodate
isodate · PyPI
Intended to create ISO time zone strings with default format hh:mm. ... A re-implementation mostly compatible with Python’s strftime, but supports only those format strings, which can also be used for dates prior 1900.
      » pip install isodate
    
Published   Oct 08, 2024
Version   0.7.2
🌐
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.