Python Forum
Calculating time difference between times and splitting the HH and MM
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calculating time difference between times and splitting the HH and MM
#7
(Jul-02-2017, 10:04 PM)ichabod801 Wrote: It seems to be calculating correctly. If the only problem is getting the hours and minutes into a string, you can calculate that from the seconds attribute of the timedelta object:

hours = countdown.seconds // 3600
minutes = (countdown.seconds // 60) % 60
print('The market opens in {} hours and {} minutes.'.format(hours, minutes))

Alternate way.

from collections import namedtuple
def to_hms(seconds):
   st_time = namedtuple('time', 'hours minutes seconds')
   minutes, seconds = divmod(seconds, 60)
   hours, minutes = divmod(minutes, 60)
   return st_time(hours, minutes, seconds)

countdown = to_hms(3660)
print(f"Sydney opens in {countdown.hours} hours and {countdown.minutes} minutes")
Output:
Sydney opens in 1 hours and 1 minutes
Btw: namedtuple is underused. But the interesting part is divmod.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
RE: Calculating time difference between times and splitting the HH and MM - by DeaD_EyE - Jul-02-2017, 11:10 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  time difference bettwenn logs enkliy 14 1,165 Nov-21-2023, 04:51 PM
Last Post: rob101
  Hard time trying to figure out the difference between two strings carecavoador 2 724 Aug-16-2023, 04:53 PM
Last Post: carecavoador
  Sum up Time difference tester_V 10 2,756 Apr-06-2023, 06:54 AM
Last Post: Gribouillis
  splitting total time between days tester_V 7 3,159 Nov-14-2020, 04:40 AM
Last Post: tester_V
  How to get indices of minimum time difference Mekala 1 2,209 Nov-10-2020, 11:09 PM
Last Post: deanhystad
  Split gps files based on time (text splitting) dervast 0 1,921 Nov-09-2020, 09:19 AM
Last Post: dervast
  How to calculate time difference between each row of dataframe in seconds Mekala 1 2,642 Jul-16-2020, 12:57 PM
Last Post: Larz60+
  Correlation of Incidents using time difference Rajhesh 1 1,873 Jun-27-2019, 03:44 PM
Last Post: Larz60+
  Time Difference in Epoch Microseconds then convert to human readable firesh 4 11,701 Feb-27-2018, 09:08 AM
Last Post: firesh

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020