2

My database table has a column whose datatype is datetime, so when queried it returns HH:MM:SS. However, using Python it returns (datetime.timedelta(0, 57360),).

This is obviously the time difference in seconds. How do I convert the returned object into a string in the format HH:MM?

Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102
Astrodude11
  • 109
  • 2
  • 5
  • 11

2 Answers2

3

What about this solution:

import datetime

td = datetime.timedelta(0, 57360)
print ':'.join(str(td).split(':')[:2])
BPL
  • 9,632
  • 9
  • 59
  • 117
1

That's how you can print the time:

import datetime

delta = datetime.timedelta(0, 57360)
sec = delta.seconds
hours = sec // 3600
minutes = (sec // 60) - (hours * 60)
print(hours, ':', minutes)

you can also print the time with seconds by

print(str(delta))
Guy Shefer
  • 88
  • 1
  • 2
  • 6
Uriel
  • 15,579
  • 6
  • 25
  • 46
  • @Astrodude11 That will work for every delta variable. The specific `delta` value comes as an example. – Uriel Sep 01 '16 at 21:45
  • @Astrodude11 `foo[0]` is `foo.days`. this is integer. call `foo.seconds` (same as `foo[1]`). – Uriel Sep 01 '16 at 22:03