11

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.

ditriquad
  • 113
  • 1
  • 1
  • 4
  • Are you looking for something like this? [timedelta](http://stackoverflow.com/questions/14190045/how-to-convert-datetime-timedelta-to-minutes-hours-in-python). – idjaw Oct 05 '15 at 23:51

1 Answers1

12

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.

lakshayg
  • 2,053
  • 2
  • 20
  • 34
amath
  • 1,279
  • 9
  • 14