Home Python Datetime Methods time() Python datetime.time() Method Definition moment.time() returns a new time object with the hour, minute, second, and microsecond of the datetime. The date and the tzinfo are dropped, so the result is always naive.
Use it to compare clock times regardless of the day, such as "is this before 09:00?". If you need to keep the time zone, use timetz() instead.
datetime.time() is an instance method of datetime.datetime: call it on an existing object with dot notation, such as moment.time(...). 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, 3, 15, 18, 45, 10).time()Returns datetime.time(18, 45, 10). datetime(2024, 3, 15, 9, 0, tzinfo=timezone.utc).time()Returns datetime.time(9, 0): no tzinfo. datetime(2024, 3, 15).time()Returns datetime.time(0, 0): midnight.
The fold attribute (used for repeated hours at daylight saving changes) is copied too.
Method signatures datetime.time()
# returns: naive time(hour, minute, second, microsecond, fold=fold) Parameters time() takes no parameters.
Return Value Return Value Description time The clock fields, with tzinfo set to None.
Python Examples 1. Clock fields 2. Zone dropped 3. Opening hours Example 1 — Take the Time Part of a datetime Extract the clock time, read its four fields, and look at its repr.
from datetime import datetime
moment = datetime(2024, 3, 15, 18, 45, 10, 250000)
clock = moment.time()
print(clock)
print(clock.hour, clock.minute, clock.second, clock.microsecond)
print(repr(clock)) 18:45:10.250000
18 45 10 250000
datetime.time(18, 45, 10, 250000) How It Works Clock only. The year, month, and day are gone; only the time prints.Attributes. hour, minute, second, and microsecond are plain ints.Microseconds shown. Because the fraction is not zero, both str() and repr() include it.Example 2 — time() Drops the Zone, timetz() Keeps It Compare the two methods on an aware datetime at UTC-04:00.
from datetime import datetime, timedelta, timezone
moment = datetime(2024, 3, 15, 18, 45, tzinfo=timezone(timedelta(hours=-4)))
print(moment.time())
print(moment.time().tzinfo)
print(moment.timetz()) 18:45:00
None
18:45:00-04:00 How It Works Same clock reading. Both methods keep 18:45; no conversion happens.No tzinfo. time() returns a naive time, so its tzinfo is None.Offset kept. timetz() keeps the zone, which prints as -04:00.Example 3 — Check Visits Against Opening Hours Compare the time part of each visit with fixed opening and closing times, whatever the date.
from datetime import datetime, time
OPEN, CLOSE = time(9, 0), time(17, 30)
visits = [
datetime(2024, 3, 15, 8, 59),
datetime(2024, 3, 15, 12, 0),
datetime(2024, 3, 16, 17, 30),
]
for visit in visits:
print(visit.strftime("%d %H:%M"), OPEN <= visit.time() < CLOSE) 15 08:59 False
15 12:00 True
16 17:30 False How It Works Time constants. time(9, 0) and time(17, 30) describe the daily window without any date.Chained comparison. OPEN <= t < CLOSE is true from 09:00 up to, but not including, 17:30.Edges. 08:59 is too early and 17:30 is exactly closing time, so both print False.Frequently Asked Questions How do I get only the time from a datetime? Call moment.time(). It returns a time object such as time(18, 45).
Why is the tzinfo missing after calling time()? time() always returns a naive time. Use timetz() to keep the time zone.
How do I compare times without dates? Compare time objects: time(9, 0) <= moment.time() < time(17, 0).
How do I format just the time? Use moment.strftime("%H:%M") or moment.time().isoformat(timespec="minutes").
Can I add minutes to a time object? No. Add a timedelta to the datetime instead, then call time() on the result.
Next: datetime.timestamp() Return the POSIX timestamp (seconds since 1970 UTC) as a float.
timestamp() → 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.
15 people found this page helpful
Helpful Share Copy link Suggestion