0

How to represent timedelta objects in 12'o clock.

from datetime import timedelta

print(timedelta(hours=13))
# 13:00:00

How to make it display in 12'o clock format like this along with AM and PM.

1:00:00 PM
ajknzhol
  • 6,322
  • 13
  • 45
  • 72

1 Answers1

2

You could use any datetime value that corresponds to midnight to perform arithmetics with timedelta:

>>> from datetime import datetime, timedelta
>>> delta24h = timedelta(hours=13)
>>> (datetime.min + delta24h).strftime('%I:%M:%S %p')
'01:00:00 PM'
jfs
  • 399,953
  • 195
  • 994
  • 1,670