convert timedelta object to time object

旧城冷巷雨未停 提交于 2021-02-18 22:23:05

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!