问题
When fetching rows from my database containing time ex 15:15:15 h/m/s. I get those in timedelta objects, i want them in time object so I later can combine them with a date object and get a datetime object.
for row in results:
startDate = row['startDate']
StartTime = row['startTime']
myListStartDate.append(datestart)
myListTimeStart.append(startTime)
When i put all the startTimes in a list and prints the list i get datetime.timedelta(0, 54900). So how do convert the timedelta to a time object so I later can compare it to other time objects.
回答1:
Here's how I would do it.
>>> import datetime
>>> startTime = datetime.timedelta(0, 54915)
>>> startTime = (datetime.datetime.min + startTime).time()
>>> startTime
datetime.time(15, 15, 15)
Credit goes to this post.
来源:https://stackoverflow.com/questions/32959604/convert-timedelta-object-to-time-object