Datetime.utcoffset() In Python

Method Name:

utcoffset

 

Method Signature:

utcoffset()

 

Method Overview:

  • utcoffset() returns the UTC Offset of the datetime instance.

 

Return Value:

  • utcoffset() returns a timedelta instance corresponding to UTC offset of the tzinfo specified in a  datetime instance

 

  • None is returned if the datetime instance does not have any tzinfo set.

 

Example:

import datetime

 

# Singapore Time

sgtTimeDelta        = datetime.timedelta(hours=8)

sgtTZObject         = datetime.timezone(sgtTimeDelta, name="SGT")

dateTimeInstance    = datetime.datetime(2017,2,14,12,15,1,99,sgtTZObject)

 

print("Singapore Time instance:{}".format(dateTimeInstance))

print("UTC Offset for Singapore Time:{}".format(dateTimeInstance.utcoffset()))

 

 

Output:

 

Singapore Time instance:2017-02-14 12:15:01.000099+08:00

UTC Offset for Singapore Time:8:00:00         

 

 


Copyright 2023 © pythontic.com