Free cookie consent management tool by TermsFeed The isoformat() method of datetime class in Python | Pythontic.com

The isoformat() method of datetime class in Python

Method Name:

isoformat

Method Signature:

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

Overview:

  • The isoformat() method of datetime class returns a date and time string which contains the following information:
    • Date
    • Time
    • UTC offset to corresponding time zone

as specified in the standard ISO 8601.

  • The separator character will be printed between the date and time fields.

Parameters:

sep        - The separator character that is to be printed between the date and time fields. This is an Optional Parameter with a dfault value of "T".

timespec   - Format specifier for the timespec. This is an Optional Parameter with a dfault value of "auto".

The values are,

auto: When the value is auto, 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: When the value is hours, the time component will have only Hours in HH format. Note that, time zone component is different from time component.

minutes: If minutes is specified, the time component will have only the Hours and Minutes printed in HH:MM format.

seconds: If seconds is specified, the time component will be printed in HH:MM:SS format.

milliseconds: If milliseconds is specified, the time component will be printed in HH:MM:SS:mmm format, where mmm is milliseconds. Microseconds will be excluded.

microseconds: If microseconds is specified, the time component will be printed in HH:MM:mmmmmm format, where mmmmmm is microseconds.

Example:

import datetime

# Local time without timezone information printed in ISO 8601 format
dateTime = datetime.datetime.today()

# Date time separator is a "#" symbol
print(dateTime.isoformat("|"))

# Create a timedelta object for CST time zone
cstTimeDelta    = datetime.timedelta(hours=-6) 

# Create a timezone instance for CST time zone
tzObject        = datetime.timezone(cstTimeDelta, name="CST")

# Replace the time zone with CST
cstTimeNow = dateTime.replace(tzinfo=tzObject)

# Print CST in ISO format - date time separator is a "#" symbol
# and time format specifier as auto
print(cstTimeNow.isoformat("#","auto"))

# Time format specifier as hours
print(cstTimeNow.isoformat("#","hours"))

# Time format specifier as minutes
print(cstTimeNow.isoformat("#","minutes"))

# Time format specifier as seconds
print(cstTimeNow.isoformat("#","seconds"))

# Time format specifier as milliseconds
print(cstTimeNow.isoformat("#","milliseconds"))

# Time format specifier as microseconds
print(cstTimeNow.isoformat("#","microseconds"))

 

Output:

2017-02-15|20:26:08.937881

2017-02-15#20:26:08.937881-06:00

2017-02-15#20-06:00

2017-02-15#20:26-06:00

2017-02-15#20:26:08-06:00

2017-02-15#20:26:08.937-06:00

2017-02-15#20:26:08.937881-06:00

 


Copyright 2025 © pythontic.com