Home Python Datetime Methods timetz() Python datetime.timetz() Method Definition moment.timetz() returns a time object with the hour, minute, second, microsecond, and the same tzinfo as the datetime. For an aware datetime, the result is an aware time.
It is the zone-keeping twin of time() . Passing the result to combine() with another date rebuilds an aware datetime in the same zone.
datetime.timetz() is an instance method of datetime.datetime: call it on an existing object with dot notation, such as moment.timetz(...). Import the class first, for example from datetime import datetime.
Syntax Call it with no arguments on a datetime.
Call What it does datetime(2024, 2, 10, 7, 15, tzinfo=timezone.utc).timetz()Returns datetime.time(7, 15, tzinfo=datetime.timezone.utc). datetime(2024, 2, 10, 7, 15).timetz()Returns datetime.time(7, 15): naive in, naive out. datetime(2024, 2, 10, 7, 15, tzinfo=timezone.utc).timetz().utcoffset()Returns datetime.timedelta(0).
No conversion happens: the clock reading stays the same, only the zone travels with it.
Method signatures datetime.timetz()
# returns: time(hour, minute, second, microsecond, tzinfo, fold=fold) Parameters timetz() takes no parameters.
Return Value Return Value Description time The clock fields together with the datetime's tzinfo (which may be None).
Python Examples 1. Zone kept 2. Aware comparisons 3. Rebuild a datetime Example 1 — Keep the Zone with the Clock Time Extract an aware time from a datetime in a named fixed zone and ask it for its name and offset.
from datetime import datetime, timedelta, timezone
berlin_winter = timezone(timedelta(hours=1), "CET")
moment = datetime(2024, 2, 10, 7, 15, tzinfo=berlin_winter)
clock = moment.timetz()
print(clock)
print(clock.tzname())
print(clock.utcoffset()) 07:15:00+01:00
CET
1:00:00 How It Works Offset printed. An aware time prints its offset after the clock, here +01:00.Name travels too. The time has the same tzinfo object, so tzname() still says CET.Offset as timedelta. utcoffset() on the time returns one hour.Example 2 — Compare Aware Times from Different Zones 13:00 UTC and 18:30 at UTC+05:30 are the same moment of the day, so the aware times compare equal.
from datetime import datetime, timedelta, timezone
ist = timezone(timedelta(hours=5, minutes=30))
utc_open = datetime(2024, 6, 1, 13, 0, tzinfo=timezone.utc).timetz()
ist_open = datetime(2024, 6, 1, 18, 30, tzinfo=ist).timetz()
print(utc_open, ist_open)
print(utc_open == ist_open)
print(utc_open.replace(tzinfo=None) == ist_open.replace(tzinfo=None)) 13:00:00+00:00 18:30:00+05:30
True
False How It Works Different readings. The two times show 13:00 and 18:30 with their own offsets.Offsets subtracted. Aware times are compared after removing the offset, and both mean 13:00 UTC.Naive comparison. Dropping the zones with replace() leaves 13:00 vs 18:30, which are not equal.Example 3 — Rebuild an Aware datetime on Another Day Reuse the aware clock time of one datetime to make the same alarm on the next day.
from datetime import date, datetime, timedelta, timezone
source = datetime(2024, 3, 15, 22, 0, tzinfo=timezone(timedelta(hours=-7)))
alarm = source.timetz()
next_day = datetime.combine(date(2024, 3, 16), alarm)
print(next_day)
print(next_day - source)
print(datetime.combine(date(2024, 3, 16), source.time()).tzinfo) 2024-03-16 22:00:00-07:00
1 day, 0:00:00
None How It Works Zone carried over. combine() copies the time's tzinfo, so the new datetime is aware at UTC-07:00.Exactly one day. Subtracting the two aware values gives a timedelta of 1 day.time() loses it. Combining with the naive time() result gives a naive datetime, so tzinfo is None.Frequently Asked Questions What is the difference between time() and timetz()? Both return the clock part. time() drops the tzinfo; timetz() keeps it.
How are aware time objects compared? Python subtracts each utcoffset() first, so 13:00+00:00 equals 18:30+05:30.
Can an aware time use daylight saving rules? Not reliably, because a time has no date. The tzinfo receives None and must return a fixed answer.
How do I attach a time zone to a naive time? Use clock.replace(tzinfo=timezone.utc).
How do I turn an aware time back into a datetime? datetime.combine(some_date, aware_time) keeps the zone automatically.
Next: date.today() Classmethod: the current local date (date.today) or naive local date and time.
today() → About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
9 people found this page helpful
Helpful Share Copy link Suggestion