The Wayback Machine - https://web.archive.org/web/20240603194709/https://www.geeksforgeeks.org/isoformat-method-of-datetime-class-in-python/
Open In App

Isoformat() Method Of Datetime Class In Python

Last Updated : 15 Oct, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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.

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 module
import datetime
import time
 
# Getting today's date
todays_Date = datetime.date.fromtimestamp(time.time())
 
# Calling the isoformat() function over the
# today's date
date_in_ISOFormat = todays_Date.isoformat()
 
# Printing Today's date in ISO format
print("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 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)


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 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 date and time format specifier
# as seconds
print(DateTime_in_ISOFormat.isoformat("#", "seconds"))
 
# Printing Today's date and time format specifier
# as milliseconds
print(DateTime_in_ISOFormat.isoformat("#", "milliseconds"))
 
# Printing Today's date and time format specifier
# as microseconds
print(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



Previous Article
Next Article

Similar Reads

Isoformat() Function Of Datetime.date Class In 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
3 min read
How to convert Python's .isoformat() string back into datetime object
In this article, we will learn how to convert Pythons .isoFormat() string back into a datetime object. Here we are using the current time and for that, we will store the current time in the current_time variable. The function now() of this module, does the job perfectly. Example: Getting current time C/C++ Code from datetime import datetime current
1 min read
Isoformat to datetime - Python
In this article, we are going to see how to convert Isoformat to datetime in Python. Method 1: Using fromisoformat() in datetime module In this method, we are going to use fromisoformat() which converts the string into DateTime object. Syntax: datetime.fromisoformatc(date) Example: Converting isoformat to datetime C/C++ Code # importing datetime mo
1 min read
Python | Pandas Timestamp.isoformat
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Timestamp objects represent date and time values, making them essential for working with temporal data. Pandas Timestamp.isoforma
2 min read
Python DateTime - DateTime Class
DateTime class of the DateTime module as the name suggests contains information on both dates as well as time. Like a date object, DateTime assumes the current Gregorian calendar extended in both directions; like a time object, DateTime assumes there are exactly 3600*24 seconds in every day. But unlike the date class, the objects of the DateTime cl
5 min read
How to convert a Python datetime.datetime to excel serial date number
This article will discuss the conversion of a python datetime.datetime to an excel serial date number. The Excel "serial date" format is actually the number of days since 1900-01-00. The strftime() function is used to convert date and time objects to their string representation. It takes one or more inputs of formatted code and returns the string r
3 min read
How to Fix - "datetime.datetime not JSON serializable" in Python?
In this article, we are going to learn how to fix the error "datetime.datetime not JSON serializable" in Python. datetime.datetime is a class in the Python datetime module that represents a single point in time. This class is not natively supported by the JSON (JavaScript Object Notation) format, which means that we cannot serialize a datetime.date
4 min read
Isoweekday() Method Of Datetime Class In Python
Isoweekday() is a method of the DateTime class that tells the day of the given date. It returns an integer that corresponds to a particular day. Syntax: datetime.isoweekday() Parameters: None Return Value: It returns an integer which corresponds to a day as per the table Integer ReturnedDay of the week1Monday2Tuesday3Wednesday4Thursday5Friday6Satur
3 min read
Isocalendar() Method Of Datetime Class In Python
The isocalendar() function is used to return a tuple of ISO Year, ISO Week Number, and ISO Weekday. Note: According to ISO standard 8601 and ISO standard 2015, Thursday is the middle day of a week.Therefore, ISO years always start with Monday.ISO year can start as yearly as 29th January or as late as 4th January of a Gregorian calendar.ISO years ca
2 min read
fromisoformat() Function Of Datetime.date Class In Python
The fromisoformat() function is used to constructs a date object from a specified string that contains a date in ISO format. i.e., yyyy-mm-dd. Syntax: @classmethod fromisoformat(date_string) Parameters: This function accepts a parameter which is illustrated below: date_string: This is the specified ISO format i.e., yyyy-mm-dd date string whose date
2 min read
Article Tags :
Practice Tags :