How do I get an ISO 8601 date in string format in Python?



The ISO 8601 standard defines an internationally recognised format for representing dates and times. ISO 8601 is a date and time format that helps remove different forms of the day, date, and time conventions worldwide.

In this article, we will discuss several methods to get an ISO 8601 date in string format in Python.

ISO 8601 Date Format

In Python, ISO 8601 date is represented as "YYYY-MM-DDTHH:MM:SS.mmmmmm" format. For example, August 25, 2023, is represented as 2023-08-25T14:35:45.123456.

  • YYYY: Year (four digits)
  • MM: Month (from 1-12)
  • DD: Days (from 1-31)
  • T: It is a character used to separate date and time fields. It is an optional parameter having a default value of "T".
  • HH: Hour (00-23 in 24-hour format)
  • MM: For the specified value of minutes
  • SS: For the specified value of seconds
  • mmmmmm: For the specified microseconds

Using the isoformat() Method

The isoformat() method returns a string format of the current datetime.date object in ISO 8601 format.

The datetime.now() method returns the current date and time in time format. To get the ISO 8601 format of it (the current local time), we need to retrieve the current local time using this method and invoke the isoformat() method on the resultant object.

Example

In the following program, we get an ISO 8601 date in string format using the .isoformat() method.

from datetime import datetime
current_date = datetime.now()
iso_format = current_date.isoformat()
print(iso_format)

Following is the output of the above code -

2025-06-16T07:29:54.299519

Using the strftime() Method

The strftime() method 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.

Example

The following program demonstrates how to get an ISO date in string format using the .strftime() method.

  • In here, we are retrieving the current date (from the local CPU) using the datetime.now() method.
  • The resultant datetime object is then converted into an ISO format string using the strftime() method.
  • Here, as we know that the ISO format is YYYY-MM-DD, we convert it into this format using the format string "%Y-%m-%dT%H:%M:%S.%f%z".
from datetime import datetime
current_date = datetime.now()
print(current_date.strftime('%Y-%m-%dT%H:%M:%S.%f%z'))

Note: The "%z" will show timezone information only if the datetime object is timezone-aware (e.g., using datetime.now(timezone.utc)).

Following is the output of the above code -

2025-06-16T07:31:24.740970

ISO 8601 Date Format of the UTC time

Previously, we have retrieved the ISO 8601 string of the current local time. To get the same for the current UTC time, we need to use the datetime.datetime.utcnow() method. This returns the datetime object representing the Coordinated Universal Time (UTC).

To get the desired string, we need to invoke the isoformat() method on the resultant object.

Example

In the following program, we retrieve the current UTC datetime using the utcnow() method and convert it to a string in ISO 8601 format using the isoformat() method.

from datetime import datetime
from datetime import datetime

# Get the current UTC datetime
current_utc_datetime = datetime.utcnow()

# Convert the datetime object to ISO 8601 string format
iso_utc_string = current_utc_datetime.isoformat()

# Print the ISO 8601 UTC string
print(iso_utc_string)

Following is the output of the above code -

2025-06-16T07:27:52.715261
Updated on: 2025-06-16T15:47:20+05:30

63K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements