'Calculating hours and minutes in python

I have a task in which i need to calculate the runtime of a marathon. I'm given this as a start point

start_hour = 3
start_minute = 48
length = 172

Basically the start is at 3:48 and it continues for 172 minutes. My task is to find when the marathon ends.The endtime should look like this format 3:48 with minutes and hour converted to string and put together with ":". I have spent like 1 and a half hour and i still can't manage to solve it. This is what i have come out with:

endhour = start_hour + (length // 60) 
endminute = start_minute + (length % 60)
end_minutee = endminute % 60
format(endhour)
endhourAsStr = str(endhour)
end_minuteeAsStr = str(end_minutee)
print(endhourAsStr + ":" + end_minuteeAsStr)

But when i print the final hour is shorter then it should be with 1 hour. I'm guessing that i need to do something with the > or < but i really can't figure it out.I guess that i only need that final push. offtopic: I'm very new to proggraming i don't have experience whatsoever.



Solution 1:[1]

You can use some datetime trickery to get a guaranteed correct result:

start_hour = 3
start_minute = 48
length = 172

start = datetime.datetime(100, 1, 1, start_hour, start_minute, 0)
end = start + datetime.timedelta(minutes=length)
result = str(end.time())

If you want to get rid of the :00 seconds at the end, simply modify the last line:

result = end.strftime('%H:%M')

I prefer this approach because it accounts for edge cases like starting one day near midnight and ending in the next day.

Solution 2:[2]

How about keeping everything in minutes and then working out the output as intended later? Keeping it simple to understand and read :)

start_time = start_hour*60 + start_minute
end_time = start_time + length
end_hour, end_minute = end_time // 60, end_time % 60
print('{}:{}'.format(end_hour, end_minute))
# 6:40

Solution 3:[3]

Use divmod.

start_hour = 3
start_minute = 48
length = 172

elapsed_hours, elapsed_minutes = divmod(length, 60)
extra_hour, finish_minute = divmod(start_minute + elapsed_minutes, 60)
finish_hour = start_hour + elapsed_hours + extra_hour
print("{hour}:{minute}".format(hour=finish_hour, minute=finish_minute))

This assumes that you don't expect anyone to finish after midnight/the next day. If you want to account for that possibility you can take the modulus of finish_hour with 24.

Solution 4:[4]

let's keep it simple

hour = int(input("Starting time (hours): "))
mins = int(input("Starting time (minutes): "))
dura = int(input("Event duration (minutes): "))

print((((hour*60+mins+dura)//60)%24) , ":" , ((mins+dura)%60) , sep="")

or

hour = int(input("Starting time (hours): "))
mins = int(input("Starting time (minutes): "))
dura = int(input("Event duration (minutes): "))

endmins = (mins+dura)%60
endhours = ((hour*60 + mins + dura)//60)%24
print(endhours , ":" , endmins , sep="")

Solution 5:[5]

Keeping it simple! :)

hour = int(input("Starting time (hours): "))

mins = int(input("Starting time (minutes): "))

dura = int(input("Event duration (minutes): "))

endmin=(mins+dura)%60

endhour=(((hour*60)+mins+dura)//60)%24

print(endhour,":",endmin)

Solution 6:[6]

Keep it short and simple

st_hour = int(input("What's the start time in hours ?"))
st_min = int(input("What's the start time in mins ?"))
du_time = int(input("What's the duration time in mins ?"))

end_hour = (st_hour + (st_min + du_time)//60)%24
end_mins = (st_min + du_time)%60

print()
print("The end time = ", end_hour, ":", end_mins)

Solution 7:[7]

hour = int(input("Starting time (hours): "))
mins = int(input("Starting time (minutes): "))
dura = int(input("Event duration (minutes): "))
new_hour=int((hour+round((dura/60)))%12)
new_mins=int((mins+(dura%60))%60)
print(new_hour,":",new_mins)

Solution 8:[8]

start_hour = int(input("Starting time (hours): "))
start_minute = int(input("Starting time (minutes): "))
length = int(input("Event duration (minutes): "))
#Calculating hours and minutes in python
end_hour = (start_hour + ((start_minute + length)//60))%24
end_mins = (start_minute + length)%60
print(end_hour,end_mins,sep=":")

This may helps

Solution 9:[9]

I suggest the following,

endhour=(start_hour*60+start_minute+length)%1440
endhour//=60
endmin=(start_minute+length)%60
print(endhour,":",endmin)

think is much simple solution :)