Isoformat() Method Of Time Class In Python

Function Name:

isoformat

Function Signature:

isoformat(timespec=”auto”)

Parameters:

timespecDefines what components of time to be included in the return value.

Return Value:

A string value containing the time with specified precision. Valid values and their meanings are given here.

 

Parameter Value

Format of the time string returned

auto

Equivalent to seconds

hours

HH:HH

minutes

HH:MM

seconds

HH:MM:SS

milliseconds

HH:MM:sss

microseconds

HH:MM:ssssss

 

Overview:

  • The method isoformat()  returns the time as a string in the format as specified by the ISO 8601 standard.

  • The return value can contain a string of minimum length 2, representing HH for hours and of maximum length 15, representing HH:MM:SS:ssssss.
  • In the similar way ISO format of a date object can be obtained using date.isoformat().

 

Example:

# ----- Example program to print datetime.date objects in ISO 8601 format

import datetime

 

# Pass hours, minutes, seconds and microseconds as parameters

pastMidnight    = datetime.time(0, 30, 2, 123456);

timeInISOFormat = pastMidnight.isoformat(timespec="microseconds");

print("Time in ISO format:%s"%timeInISOFormat);

 

# Display only upto minutes....let other components like seconds and microseconds be truncated

timeInISOFormat = pastMidnight.isoformat(timespec="minutes");

print("Time with up to minutes part:%s"%timeInISOFormat);

 

Output:

Time in ISO format:00:30:02.123456

Time with up to minutes part:00:30

 


Copyright 2023 © pythontic.com