This Python AttributeError about datetime.time appears when code calls the time() method on the wrong object.
What This Datetime Attributeerror Message Means
When Python raises attributeerror: type object 'datetime.time' has no attribute 'time', it is telling you that you are calling the time() attribute on the datetime.time class itself instead of on something that actually has that method.
The datetime module has several types. The datetime.datetime class represents a full date and time and supplies a .time() method that returns only the clock part. The datetime.time class already represents only a time of day, so it does not need another .time() method. When your code calls datetime.time.time(), Python looks for a method named time on the datetime.time class and fails.
This error message often comes from code that mixes two separate ideas. One is the datetime module for calendar and clock values. The other is the time module that holds the time() function for Unix timestamps. Understanding that difference makes debugging this message much easier.
Tracebacks from this problem usually show a short chain of calls that ends at the line using datetime.time.time(). Reading that last frame tells you which file and which function are misusing the class, so you can jump straight to the right place in your editor.
- Class versus instance The wording
type objecttells you that the attribute access happens on a class, not on an instance. - Missing attribute The phrase
has no attribute 'time'means there is no method or attribute by that name defined on that class. - Wrong target Because
datetime.timeis already a time-of-day type, calling.time()on it does not match how the standard library is designed.
Why You See AttributeError: Type Object ‘Datetime.Time’ Has No Attribute ‘Time’
This error almost always comes from a short block of code that looks harmless at first glance. Walking through the usual patterns helps you match them to your project quickly.
Calling datetime.time.time() For A Timestamp
A common pattern is code that tries to read the current Unix timestamp like this:
import datetime
timestamp = datetime.time.time()
This line mixes two separate modules. The time() function that returns a floating point timestamp lives in the time module, not inside datetime.time. Python tries to treat datetime.time as if it had that function and then raises the AttributeError.
It often slips in when a developer has seen time.time() in older code and later switches to datetime for richer date handling. Habit takes over, and the call becomes datetime.time.time() while the underlying objects are not the same.
Confusing datetime.datetime And datetime.time
Another pattern appears when code tries to peel the time of day from a datetime but takes the wrong route:
import datetime
t = datetime.time
current = t.time()
The intention is clear, yet the call attaches .time() to the class alias t, not to a full datetime value. The correct pattern calls .time() on a datetime.datetime instance instead.
Confusion also shows up when developers shorten names too aggressively. Aliases like TimeClass for datetime.time or DT for datetime.datetime hide the link to the original types. When you later scan a stack trace, it becomes harder to see that the AttributeError points back to those aliases.
- Check your imports Scan the top of the file and see whether you are importing the
timemodule at all. - Search for datetime.time.time Use your editor search to catch any direct
datetime.time.time()calls. - Track helpers and aliases If you assign
t = datetime.time, check each latert.time()call, because each one can trigger this AttributeError.
Fixing Attributeerror Type Object Datetime.Time Has No Attribute Time In Real Code
Once you see where the wrong call lives, the fix usually comes down to choosing the right object. That choice depends on whether you want a timestamp, a pure time-of-day value, or a full date and time.
Fix 1: Use The time Module For Timestamps
If your goal is to fetch a Unix timestamp, switch to the time module instead of the datetime.time class.
import time
timestamp = time.time()
This call asks the time module directly for the current timestamp. There is no reference to datetime.time, so attributeerror: type object 'datetime.time' has no attribute 'time' never appears.
You can also reach timestamps through datetime if you prefer that style. The datetime.datetime class exposes a .timestamp() method that returns the same basic float value.
from datetime import datetime
now = datetime.now()
timestamp = now.timestamp()
Fix 2: Call .time() On A datetime.datetime Instance
If you already have a datetime value and want only the clock part, call .time() on that instance.
from datetime import datetime
now = datetime.now()
clock_time = now.time()
The datetime.datetime type defines the .time() method, so this code works as intended. You can then pass clock_time around as a datetime.time instance without calling .time() on it again.
When you log values or send them across a network boundary, serializing clock_time to a string with .isoformat() or format codes gives you a clear representation that can be reconstructed later.
Fix 3: Drop Redundant .time() Calls
In some projects the bug sits in code that already has a datetime.time instance but still calls .time() on it indirectly.
from datetime import time
start = time(9, 0)
# Wrong: start.time() would fail if called
meeting_start = start # No .time() call needed
Here, start already stores a datetime.time instance. Passing that variable around is enough. Extra .time() calls simply are not required and may lead to the AttributeError if added later.
When you review pull requests or older modules, a quick scan for patterns that call .time() on values that are already datetime.time instances can reveal fragile spots that deserve a small clean up.
- Choose the right type Decide whether your code needs a timestamp, a time of day, or a full date with time.
- Match methods to types Call
.time()only ondatetime.datetimeinstances, not on thedatetime.timeclass. - Keep timestamps separate Use the
timemodule ordatetime.datetime.timestamp()for numeric timestamps instead ofdatetime.time.
Using The Correct Module For Timestamps And Clocks
The easiest way to avoid this AttributeError in new code is to give each module a clear role and stick to that habit. The datetime module handles calendar dates and clock times. The time module handles timestamps, sleep, and simple timing.
Many projects mix those two modules in the same file. That is not wrong by itself, yet it raises the chances of writing datetime.time.time() by accident. Clear naming and import style cut down that risk.
Library code that has to deal with time zones or daylight saving rules often sticks with datetime objects from end to end. That keeps offsets attached to each value and avoids subtle bugs when clocks shift during the year.
Clear Import Patterns
These import styles keep the difference between modules visible in your code.
import datetime
import time
now = datetime.datetime.now()
now_time = now.time()
ts = time.time()
Another common pattern shortens only the datetime class name.
from datetime import datetime
import time
now = datetime.now()
now_time = now.time()
ts = time.time()
Some teams prefer to keep the full module names to reduce confusion in shared projects. Others combine both patterns, importing the module and the class, then referring to them in different contexts. Pick one approach for each file and stick with it so that a reader can guess which object each name refers to.
- Keep module names clear Avoid aliasing
datetime.timeastime, because it hides the difference from the realtimemodule. - Group related imports Place
datetimeandtimeimports near each other, then scan them when you see any AttributeError around time handling. - Review helper functions When helpers wrap date or time logic, confirm that they call the right module functions before you reuse them.
Safer Patterns For Working With Dates And Times
This error tends to appear when a code base grows and many helpers start to share date and time logic. A few small habits make that growth smoother and cut down surprises at runtime.
Strong naming, narrow helper functions, and simple tests around time handling go a long way here. You do not need a large rewrite; a few direct habits around naming and imports already reduce confusion.
In code reviews, questions around time handling pay off quickly. When someone proposes a change that adds yet another helper, ask which type each argument expects and which type each return value carries. That small habit keeps mistakes around .time() and timestamps from spreading across modules.
Name Variables By What They Hold
When you read code months later, names like now_dt, today_date, or start_time make it clear which type you are dealing with. That habit also helps newcomers understand that datetime.time is a type, while time.time() is a function.
Keep Conversions In One Place
Many bugs come from code that converts between timestamps, date objects, and time-of-day values in several scattered spots. Moving conversions into small helpers makes errors easier to spot and keeps .time() calls pinned to the right objects.
from datetime import datetime
import time
def current_timestamp() -> float:
return time.time()
def current_time_of_day() -> datetime.time:
return datetime.now().time()
- Use descriptive names Let names hint at whether a value is a timestamp, a date, or a time of day.
- Centralize conversions Route conversions through a handful of helpers instead of scattering them through many files.
- Add small tests Even a short test suite that checks these helpers prevents regressions and keeps AttributeError messages away.
Quick Reference Table For This Datetime Error
When you run into a message about datetime.time having no time attribute in a log or stack trace, this quick reference table helps you map the symptom to a clean fix.
| Symptom | Cause | Fix |
|---|---|---|
datetime.time.time() in code |
Calling time() on the datetime.time class |
Import time and call time.time() |
Alias to datetime.time then alias.time() |
Using an alias for the class and treating it like an instance | Create a datetime.datetime instance and call .time() on that object |
Helper returns datetime.time but caller adds .time() |
Extra method call added on a value that is already a datetime.time instance |
Remove the extra .time() call and pass the instance directly |
A scratch script that imports your project and calls the same functions from the Python REPL can help. Running the code step by step lets you inspect the actual types of intermediate values with type() or isinstance() before they reach the line that fails.
Once you understand how the datetime and time modules split their duties, this AttributeError turns into a simple signal. It tells you that a .time() method landed on the wrong object, so you only need to bring that call back to either a datetime.datetime instance or the dedicated time module.
