I am trying to create a time object to save to my database. Here is what I have so far:
>>> minutes=137
>>> import datetime
>>> datetime.time(minute=minutes)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: minute must be in 0..59
How would I create a time object with more than 59 minutes?
datetime.time()
objects model a time of day , not a duration . Unless you want your 137 minutes to be interpreted as 137 minutes after midnight , you should not be using datetime.time()
objects.
Use datetime.timedelta()
for durations:
>>> import datetime
>>> minutes = 137
>>> datetime.timedelta(minutes=minutes)
datetime.timedelta(0, 8220)
The timedelta
object tracks time durations in days and seconds.
If you did mean for the time to be interpreted as 2:17am you'll need to calculate the hours and remainder:
>>> hour, minute = divmod(minutes, 60)
>>> datetime.time(hour, minute)
datetime.time(2, 17)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.