Python datetime.utcoffset() Method

Beginner
6 min read
Updated: Nov 2026
3 examples
datetime method

Definition

utcoffset() returns how far the datetime's local time is ahead of UTC, as a timedelta: 5:30:00 for UTC+05:30. Zones west of UTC give negative values.

For a naive datetime it returns None. Otherwise it calls self.tzinfo.utcoffset(self), so a custom tzinfo can return different offsets for winter and summer. The offset includes any daylight saving shift reported by dst().

datetime.utcoffset() is an instance method of datetime.datetime: call it on an existing object with dot notation, such as moment.utcoffset(...). Import the class first, for example from datetime import datetime. The time class has a method with the same name.

Syntax

Call it with no arguments.

python
offset = moment.utcoffset()
CallWhat it does
datetime(2024, 3, 15, tzinfo=timezone(timedelta(hours=5, minutes=30))).utcoffset()Returns datetime.timedelta(seconds=19800).
datetime(2024, 3, 15, tzinfo=timezone(timedelta(hours=-5))).utcoffset()Returns datetime.timedelta(days=-1, seconds=68400), which prints as -1 day, 19:00:00.
datetime(2024, 3, 15).utcoffset()Returns None: the value is naive.

time.utcoffset() works the same way for aware time objects, and tzinfo.utcoffset(dt) is the method a custom zone implements.

Method signatures
python
datetime.utcoffset()
time.utcoffset()
tzinfo.utcoffset(dt)        # implemented by tzinfo subclasses
# returns: timedelta strictly between -24h and +24h, or None

Parameters

utcoffset() takes no parameters on datetime and time objects.

Return Value

Return ValueDescription
timedeltaLocal time minus UTC for an aware value.
NoneFor a naive value, or when the tzinfo returns None.

Python Examples

Example 1 — Read the Offset of Three datetimes

A naive value, a UTC value, and a zone at UTC-03:30.

python
from datetime import datetime, timedelta, timezone

samples = [
    datetime(2024, 3, 15, 10, 30),
    datetime(2024, 3, 15, 10, 30, tzinfo=timezone.utc),
    datetime(2024, 3, 15, 10, 30, tzinfo=timezone(timedelta(hours=-3, minutes=-30))),
]
for moment in samples:
    print(moment.utcoffset())

How It Works

  1. Naive gives None. No tzinfo means no offset at all.
  2. UTC gives zero. timedelta(0) prints as 0:00:00.
  3. Negative offsets. Minus 3 hours 30 minutes is stored as -1 day plus 20:30:00, the normalized timedelta form.

Example 2 — Test Whether a datetime Is Aware

The official rule: a datetime is aware when tzinfo is set and utcoffset() does not return None. Then read an offset in hours.

python
from datetime import datetime, timedelta, timezone


def is_aware(moment):
    return moment.tzinfo is not None and moment.utcoffset() is not None


print(is_aware(datetime(2024, 1, 1)))
print(is_aware(datetime(2024, 1, 1, tzinfo=timezone.utc)))
nepal = timezone(timedelta(hours=5, minutes=45))
offset = datetime(2024, 1, 1, tzinfo=nepal).utcoffset()
print(offset.total_seconds() / 3600)

How It Works

  1. Two-part check. A tzinfo could still return None, so the helper checks both conditions.
  2. Naive vs aware. The plain datetime fails the test; the UTC one passes.
  3. Hours as a float. total_seconds() of 5:45:00 is 20700.0, which is 5.75 hours.

Example 3 — Convert to UTC by Subtracting the Offset

Subtract the offset from the clock reading yourself and compare with astimezone(timezone.utc).

python
from datetime import datetime, timedelta, timezone

local = datetime(2024, 3, 15, 9, 0, tzinfo=timezone(timedelta(hours=-5), "EST"))
offset = local.utcoffset()
manual_utc = local.replace(tzinfo=None) - offset

print(offset)
print(manual_utc)
print(manual_utc == local.astimezone(timezone.utc).replace(tzinfo=None))

How It Works

  1. Offset of EST. UTC-05:00 prints as -1 day, 19:00:00.
  2. UTC = local - offset. 09:00 minus (minus 5 hours) is 14:00 UTC.
  3. Same as astimezone(). The built-in conversion gives the same clock reading once its zone is removed.

Frequently Asked Questions

Call moment.utcoffset(). It returns a timedelta, or None for naive values.
That is how timedelta shows minus 5 hours: one negative day plus 19 positive hours.
moment.utcoffset().total_seconds() / 3600.
moment.tzinfo is not None and moment.utcoffset() is not None.
Use moment.strftime("%z").

Next: datetime.utctimetuple()

Return a struct_time in UTC: aware values are converted, the DST flag is 0.

utctimetuple() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

15 people found this page helpful