Evaluating the end time of a period of time
hour = int(input("Starting time (hours): "))
mins = int(input("Starting time (minutes): "))
dura = int(input("Event duration (minutes): "))
time_hour = (hour + dura//60 + (mins+ dura%60)//60)%24
time_min = (mins+ dura%60)%60
print("It will end at " + str(time_hour) + ":" + str(time_min))
It will end at 13:16
Try this:
from datetime import date, datetime, time, timedelta
if __name__ == '__main__':
hour = int(input("Starting time (hours): "))
mins = int(input("Starting time (minutes): "))
dura = int(input("Event duration (minutes): "))
dt = datetime.combine(date.today(), time(hour, mins)) + timedelta(minutes=dura)
print(dt.time().strftime("%H:%M"))
Output: 13:16
I understand this has been answered. However I ran across this exact problem in some practice work for learning python. The end result just wanted the time in HH:mm format.
#given hour = 12 , mins = 17 , dura = 59
hour = int(input("Starting time (hours): "))
mins = int(input("Starting time (minutes): "))
dura = int(input("Event duration (minutes): "))
hour = (hour + dura//60 + (mins+ dura%60)//60)%24
mins = (mins+ dura%60)%60
print(hour,":",mins,sep="")
13:16
The last line was requested like this. I understand this was largely explained and answered, but I saw a difference in syntax and wanted to share my results.