Home »
Python »
Python Reference »
Python date Class
Python date isoformat() Method with Example
Python date.isoformat() Method: Here, we are going to learn about the isoformat() method of date class in Python with its definition, syntax, and examples.
Submitted by Hritika Rajput, on April 29, 2020
Python date.isoformat() Method
date.isoformat() method is used to manipulate objects of date class of module datetime.
It uses a date class object and returns a string representing the date in ISO 8601 format, YYYY-MM-DD. The International Standard for the representation of dates and times is ISO 8601. It was designed to provide a format of date and time representation free from any ambiguity. The format provides a standard approach and has the following rules:
- Year first, followed by month, then the day, each separated by a hyphen ("-")
- Numbers less than 10 preceded by a leading zero
Module:
import datetime
Class:
from datetime import date
Syntax:
isoformat()
Parameter(s):
Return value:
The return type of this method is a string in ISO 8601 format of the date.
Example:
## importing date class
from datetime import date
## Creating an instance
x = date.today()
d = x.isoformat()
print("Normal format:",x)
print("ISO 8601 format:", d)
print()
x = date(2020,10,1)
print("Date 2020/10/1 in ISO 8601 format:", x.isoformat())
print()
x = date(200,10,12)
print("Date 200/10/12 in ISO 8601 format:", x.isoformat())
print()
x = date(1,1,1)
print("Date 1/1/1 in ISO 8601 format:", x.isoformat())
Output
Normal format: 2020-04-29
ISO 8601 format: 2020-04-29
Date 2020/10/1 in ISO 8601 format: 2020-10-01
Date 200/10/12 in ISO 8601 format: 0200-10-12
Date 1/1/1 in ISO 8601 format: 0001-01-01