Hi,
Can you help with a time comparrison quesion in Python 3.6?
I want to calculate the hours and minutes between two time and then display the difference,, by "splitting" the hours and minutes into a text string along the lines of "The market opens in HH hours and MM minutes"
The solution seems to start off with putting both times into the same format, so I can subtract one from the other. This will handle the minutes element correctly.
The difficulty is being able to "split" the result into Hours and Minutes, to display the difference into the required text string "The market opens in HH hours and MM minutes" with time.split but this fails with a str error.
If I extract the hour elements and then the minute elements from both times and then compare them, this causes problems with carrying over the minutes.
I have tried to manipulate the output "HH:MM", but have failed.
Any help would be appreciated.
Thank you
Bass
Can you help with a time comparrison quesion in Python 3.6?
I want to calculate the hours and minutes between two time and then display the difference,, by "splitting" the hours and minutes into a text string along the lines of "The market opens in HH hours and MM minutes"
The solution seems to start off with putting both times into the same format, so I can subtract one from the other. This will handle the minutes element correctly.
The difficulty is being able to "split" the result into Hours and Minutes, to display the difference into the required text string "The market opens in HH hours and MM minutes" with time.split but this fails with a str error.
If I extract the hour elements and then the minute elements from both times and then compare them, this causes problems with carrying over the minutes.
I have tried to manipulate the output "HH:MM", but have failed.
Any help would be appreciated.
Thank you
Bass
import datetime as datetime local_time = datetime.datetime.now() # The Sydney Opening time is 22H00M # This time is put into a valid time string (via .replace) # in order to subtract the number of hours and minutes until the # market opens in this line of code: countdown = sydney_open - local_time # This calculates the correct difference in hours/minutes. But prevents me # from splitting the result into hours and then minutes. # One alternative could be to multiply the hours by 60 and adding the # minutes element, for both times (current and opening time), then # calculating the hours by dividing by 60 and then deriving the # minutes to provide a result in hours and minutes. But this is # converluted - so looking for a smarter method. sydney_open = datetime.datetime.strptime('22:00:00', "%H:%M:%S") sydney_open = local_time.replace(hour=sydney_open.time().hour, minute=0, second=0, microsecond=0) countdown = sydney_open - local_time print("local_time = ", local_time) print("sydney_open = ", sydney_open) print("Countdown....", countdown) # I want to display the countdown as "The Market opens in HH hours and MM minutes # Currently the output is: Sydney opens in 2:04:34.950393 # The preferred output is "Sydney opens in 2 hours and 4 minutes"
"The good thing about standards is that you have so many to choose from" Andy S. Tanenbaum