utcoffset() method of timezone class in Python

The utcoffset() method returns the UTC offset of a timezone information as a timedelta object.

Timezone Updated 9 Aug 2021 1 min read

Overview

  • UTC offset is the time difference between one place on the Earth and the UTC.
  • The method utcoffset() returns the UTC offset for a timezone instance.

Example

python
# Example Python program that prints the UTC offset

            # from a timezone instance

            from datetime import timedelta, timezone, time

# Create a timedelta with +9 hours as duration

            jstOffset = timedelta(hours = 9)

            jst = timezone(jstOffset, "Japanese Standard Time")

# Print the UTC offset

            print("UTC offset from time zone:")

oft = jst.utcoffset(None)

            print(oft)

            print(type(oft))

# Japanese afternoon

            jpAfternoon = time(hour = 13, tzinfo = jst)

            print("Time:")

            print(jpAfternoon.__format__("%I:%M %p JST"))

Output

Output
UTC offset from time zone:

9:00:00

<class 'datetime.timedelta'>

Time:

01:00 PM JST