Isoformat() Method Of Datetime Class In Python

15 Oct, 2021
In this example, we will learn How to get date values in ISO 8601 format using Python. The Isoformat() function is used to return a string of date, time, and UTC offset to the corresponding time zone in ISO 8601 format.
The standard ISO 8601 format is all about date formats for the Gregorian calendar. This format prescribes that a calendar date needs to be represented using a 4-digit year followed by a two-digit month and a two-digit date. i.e., YYYY-MM-DD. Example: 2020-01-01.
Syntax: isoformat(sep=’T’, timespec=’auto’)
Parameters: This function accepts two parameters which are illustrated below:
sep: It is the separator character that is to be printed between the date and time fields. It is an Optional Parameter having default value of “T”.
timespec: It is the format specifier for the timespec. It is also an Optional Parameter with a default value of “auto”. This parameter is also having some values that are illustrated below:auto: For the auto value, the time component will be printed in HH:MM:SS format. If microseconds component is available it will be printed. Otherwise, microseconds will be omitted instead of printing as zero.hours: For the hours value, the returned time component will have only Hours in HH format. Note that, time zone component is different from time component.minutes: For the specified value of minutes, the returned time component will have only the Hours and Minutes printed in HH:MM format.seconds: For the specified value of seconds, the returned time component will have HH:MM:SS format.milliseconds: For the specified value of milliseconds, the returned time component will have HH:MM:SS:mmm format, where mmm is milliseconds. Microseconds will be excluded.microseconds: For the specified microseconds, the returned time component will have HH:MM:mmmmmm format, where mmmmmm is microseconds.
auto: For the auto value, the time component will be printed in HH:MM:SS format. If microseconds component is available it will be printed. Otherwise, microseconds will be omitted instead of printing as zero.
hours: For the hours value, the returned time component will have only Hours in HH format. Note that, time zone component is different from time component.
minutes: For the specified value of minutes, the returned time component will have only the Hours and Minutes printed in HH:MM format.
seconds: For the specified value of seconds, the returned time component will have HH:MM:SS format.
milliseconds: For the specified value of milliseconds, the returned time component will have HH:MM:SS:mmm format, where mmm is milliseconds. Microseconds will be excluded.
microseconds: For the specified microseconds, the returned time component will have HH:MM:mmmmmm format, where mmmmmm is microseconds.
Return values: This function returns the date value of a Python DateTime.date object in ISO 8601 format.
Example 1: In the below example, the isoformat() function has been called on today’s date and it returns the same today’s date string in ISO 8601 format.
Python3
# Python3 code to demonstrate# Getting date values in ISO 8601 format # importing datetime and time moduleimport datetimeimport time # Getting today's datetodays_Date = datetime.date.fromtimestamp(time.time()) # Calling the isoformat() function over the# today's datedate_in_ISOFormat = todays_Date.isoformat() # Printing Today's date in ISO formatprint("Today's date in ISO Format: %s" % date_in_ISOFormat)
Output:
Today’s date in ISO Format: 2021-07-27
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
# Python3 code to demonstrate# Getting date and time values# in ISO 8601 format # importing datetime and time moduleimport datetimeimport time # Getting today's date and timetodays_Date = datetime.datetime.now() # Calling the isoformat() function over the# today's date and timeDateTime_in_ISOFormat = todays_Date.isoformat() # Printing Today's date and time in ISO formatprint("Today's date and time in ISO Format: %s" % DateTime_in_ISOFormat)
Output:
Today’s date and time in ISO Format: 2021-07-27T16:02:08.070557
In the below example, the isoformat() function has taken two parameters one is separator character such as ‘#’ and another parameter is format specifier for the time-specific. But if different values for time specifiers are used the output can be formatted according to that. 
Example 3: Here different values for the time-specific parameter are used that are already illustrated in the above parameter section.
Python3
# Python3 code to demonstrate# Getting date and time values# in ISO 8601 format # importing datetime moduleimport datetime # Getting today's date and timeDateTime_in_ISOFormat = datetime.datetime.now() # Printing Today's date and time in ISO format of# auto value for the format specifierprint(DateTime_in_ISOFormat.isoformat("#", "auto")) # Printing Today's date and time format specifier# as hoursprint(DateTime_in_ISOFormat.isoformat("#", "hours")) # Printing Today's date and time format specifier# as minutesprint(DateTime_in_ISOFormat.isoformat("#", "minutes")) # Printing Today's date and time format specifier# as secondsprint(DateTime_in_ISOFormat.isoformat("#", "seconds")) # Printing Today's date and time format specifier# as millisecondsprint(DateTime_in_ISOFormat.isoformat("#", "milliseconds")) # Printing Today's date and time format specifier# as microsecondsprint(DateTime_in_ISOFormat.isoformat("#", "microseconds"))
Output:
2021-07-27#16:01:12.090202
2021-07-27#16
2021-07-27#16:01
2021-07-27#16:01:12
2021-07-27#16:01:12.090
2021-07-27#16:01:12.090202
surindertarika1234
Picked
Python-datetime
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.