ADVERTISEMENT
ADVERTISEMENT

Python datetime isoformat() Method with Example

Python datetime.isoformat() Method: Here, we are going to learn about the isoformat() method of datetime class in Python with its definition, syntax, and examples.
Submitted by Hritika Rajput, on May 02, 2020

Python datetime.isoformat() Method

datetime.isoformat() method is used to manipulate objects of datetime class of module datetime.

It uses a datetime class object and returns a 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:

    import datetime

Class:

    from datetime import datetime

Syntax:

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

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:

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



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.