The datetime.utcoffset() in Python

Method Name:

utcoffset

Method Signature:

utcoffset()

Method Overview:

  • The utcoffset() method returns the UTC offset of a datetime instance.

Return Value:

  • The 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 2025 © pythontic.com