0
diff = reference_time - topic_time
hour = round((reference_time-topic_time) / datetime.timedelta(hours=1))

if reference_time = '2020-08-23 07:00:10' and topic_time = '2020-08-22 00:00:00', the 'diff' variable is:

days = 1
seconds = 25210

The 'hour' conversion code make the hour = 31, which seems incorrect. The max diff should be less than 24 hours in one day. How to calculate time diffs and convert to hours in this case?

marlon
  • 6,029
  • 8
  • 42
  • 76

1 Answers1

0
import datetime

firstTime = datetime.datetime.utcnow()
secondTime = datetime.datetime.utcnow() + datetime.timedelta(hours=5)
diff = secondTime - firstTime
hours = diff.total_seconds() // 3600
print(hours) # Answer is 5


reference_time = datetime.datetime(2020,8,23,7)
topic_time = datetime.datetime(2020,8,22,0)
hours = (reference_time - topic_time).total_seconds() // 3600
days = hours // 24
hours = hours - (days*24)
print('days: %d, hours: %d' % (days, hours) ) # days:1, hours:7
bilginyuksel
  • 160
  • 2
  • 7