Python datetime.time() Method

Beginner
6 min read
Updated: Nov 2026
3 examples
datetime 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.

python
clock = moment.time()
CallWhat 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
python
datetime.time()
# returns: naive time(hour, minute, second, microsecond, fold=fold)

Parameters

time() takes no parameters.

Return Value

Return ValueDescription
timeThe clock fields, with tzinfo set to None.

Python Examples

Example 1 — Take the Time Part of a datetime

Extract the clock time, read its four fields, and look at its repr.

python
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))

How It Works

  1. Clock only. The year, month, and day are gone; only the time prints.
  2. Attributes. hour, minute, second, and microsecond are plain ints.
  3. 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.

python
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())

How It Works

  1. Same clock reading. Both methods keep 18:45; no conversion happens.
  2. No tzinfo. time() returns a naive time, so its tzinfo is None.
  3. 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.

python
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)

How It Works

  1. Time constants. time(9, 0) and time(17, 30) describe the daily window without any date.
  2. Chained comparison. OPEN <= t < CLOSE is true from 09:00 up to, but not including, 17:30.
  3. Edges. 08:59 is too early and 17:30 is exactly closing time, so both print False.

Frequently Asked Questions

Call moment.time(). It returns a time object such as time(18, 45).
time() always returns a naive time. Use timetz() to keep the time zone.
Compare time objects: time(9, 0) <= moment.time() < time(17, 0).
Use moment.strftime("%H:%M") or moment.time().isoformat(timespec="minutes").
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

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