Content · Programming · Python

Python datetime.time Class: Usage and Examples

Represent a clock time without coupling it to a calendar date.

2 views Published Oct 17, 2026 Updated Oct 18, 2026 Admin

Represent a clock time without coupling it to a calendar date.

How It Works


datetime.time(hour, minute, second, microsecond, tzinfo) stores time-of-day components and optionally an offset. A standalone time object does not identify a unique instant; date and location may be needed to resolve daylight saving changes.

Step-by-Step Example


from datetime import time
opening_time = time(8, 45, 30)
print(opening_time.isoformat())
print(opening_time.hour)
print(opening_time.minute)

Understanding the Example


  • The expression 'time(8, 45, 30)' supplies the value stored as 'opening_time'. This value is then used or checked by the later statements.
  • The print() call exposes the result so you can compare it with the behavior described above. Check the value and its type when experimenting.

A successful run of this example prints the following output:

08:45:30
8
45

Where This Fits in a Real Program


For event timestamps, distinguish the displayed local clock from the instant that it represents. Store or exchange an unambiguous aware value, then convert it when displaying it to a user. For repeated calendar appointments, use a geographic timezone so daylight-saving rules are not lost.

Practice: Try It Yourself


Use datetime.combine() when you need a time on a specific date.

For this exercise, predict the result before running the changed code. Then explain the difference between the original and modified cases using the actual values, not just whether the program completed.

Further Reading


Read the official Python documentation for complete parameters, return types and exceptions.

Common Beginner Mistakes


  • Assuming a standalone time identifies an exact moment without a date and time zone.
  • Create opening and closing times and compare their hour and minute fields.

Key Takeaways


  • Represent a clock time without coupling it to a calendar date.
  • datetime.time(hour, minute, second, microsecond, tzinfo) stores time-of-day components and optionally an offset.
  • Apply the demonstrated operation to the practice input and verify its result before depending on it in a larger program.
Share this resourceSend this page to someone who may find it useful.