Python date isoformat() Method with Example

Python date.isoformat() Method: In this tutorial, we will learn about the isoformat() method of date class in Python with its usage, syntax, and examples. By Hritika Rajput Last updated : April 22, 2023

Python date.isoformat() Method

The date.isoformat() method 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:

  1. Year first, followed by month, then the day, each separated by a hyphen ("-")
  2. Numbers less than 10 preceded by a leading zero

Module

The following module is required to use isoformat() method:

import datetime

Class

The following class is required to use isoformat() method:

from datetime import date

Syntax

The following is the syntax of isoformat() method:

isoformat()

Parameter(s)

The following are the parameter(s):

  • None

Return Value

The return type of this method is a string in ISO 8601 format of the date.

Example of date isoformat() Method in Python

## 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



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.