Python datetime isoformat() Method with Example

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

Python datetime.isoformat() Method

The datetime.isoformat() method is used to get the string representing the date and time in ISO 8601 format:

  • YYYY-MM-DDTHH:MM:SS.ffffff, if microsecond is not 0
  • YYYY-MM-DDTHH:MM:SS, if microsecond is 0

If the datetime is an aware object, then a string is added which tells with UTC offset:

  • YYYY-MM-DDTHH:MM:SS.ffffff+HH:MM[:SS[.ffffff]], if microsecond is not 0
  • YYYY-MM-DDTHH:MM:SS+HH:MM[:SS[.ffffff]], if microsecond is 0

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.

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 datetime

Syntax

The following is the syntax of isoformat() method:

isoformat(sep='T', timespec='auto')

Parameter(s)

The following are the parameter(s):

  • sep: is a one-character separator, placed between the date and time portions of the result. The default value is T.
  • timespec: is the number of additional components of the time to include. Its default value is auto. It is same as 'seconds' if microsecond is 0, or otherwise, it is microsecond.

Both of these parameters are optional.

Return Value

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

Example to get the string representing the date and time in ISO 8601 format in Python

## importing datetime class
from datetime import datetime
import pytz

## Naive object isoformat() example

x = datetime.today()
print("Normal format:",x)
d = x.isoformat()
print("ISO 8601 format:", d)
print("ISO 8601 format without separator 'T':", x.isoformat(sep=' '))
print()

## Microsecond is not 0 here
x = datetime(2020, 10, 1, 12, 12, 12, 3400)
print("ISO 8601 format:", x.isoformat())
print("ISO format without separator 'T':", x.isoformat(sep=' '))
print()

## Microsecond is 0 here
x = datetime(200,10,12,1,1,1)
print("Date 200/10/12 1:1:1 in ISO 8601 format:", x.isoformat())
print()

## Aware object isoformat example

##Microsecond is 0 here
x = datetime(2020,10,4)
timezone = pytz.timezone("Asia/Tokyo")
x = x.astimezone(timezone)##Adding tzinfo
print("ISO 8601 format for an aware object:", x.isoformat())

## Microsecond is not 0 here
x = datetime(2020,10,4,1,1,1,333)
timezone = pytz.timezone("Asia/Tokyo")
x = x.astimezone(timezone)##Adding tzinfo
print("ISO 8601 format for an aware object:", x.isoformat())

Output

Normal format: 2020-05-01 22:52:37.596841
ISO 8601 format: 2020-05-01T22:52:37.596841
ISO 8601 format without separator 'T': 2020-05-01 22:52:37.596841

ISO 8601 format: 2020-10-01T12:12:12.003400
ISO format without separator 'T': 2020-10-01 12:12:12.003400

Date 200/10/12 1:1:1 in ISO 8601 format: 0200-10-12T01:01:01

ISO 8601 format for an aware object: 2020-10-04T09:00:00+09:00
ISO 8601 format for an aware object: 2020-10-04T10:01:01.000333+09:00



Comments and Discussions!

Load comments ↻





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