1

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
  • 18,661
  • 20
  • 81
  • 93
Astrodude11
  • 87
  • 1
  • 4
  • 9

2 Answers2

1

What about this solution:

import datetime

td = datetime.timedelta(0, 57360)
print ':'.join(str(td).split(':')[:2])
BPL
  • 9,532
  • 7
  • 47
  • 105
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
  • 86
  • 1
  • 6
Uriel
  • 14,803
  • 6
  • 23
  • 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