5

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.

ilio
  • 53
  • 1
  • 1
  • 5

10 Answers10

5

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.

stelioslogothetis
  • 9,371
  • 3
  • 28
  • 53
  • As for why a dummy date (`100, 1, 1`) must be used: https://stackoverflow.com/questions/100210/what-is-the-standard-way-to-add-n-seconds-to-datetime-time-in-python – mkrieger1 Aug 14 '17 at 11:54
  • 2
    Formatting can also more explicitly be done using `result = end.strftime('%H:%M')`. – mkrieger1 Aug 14 '17 at 11:59
  • @mkrieger1 You are correct. I have edited my answer. – stelioslogothetis Aug 14 '17 at 12:03
  • Or even better, [using `str.format`](https://stackoverflow.com/questions/2158347/how-do-i-turn-a-python-datetime-into-a-string-with-readable-format-date/22842734#22842734) – mkrieger1 Aug 14 '17 at 12:06
3

I would suggest

endhour = start_hour + (length // 60) 
endminute = start_minute + (length % 60)
endhour += endminute // 60
endminute = endminute % 60
endhour = endhour % 24 

print('{}:{}'.format(endhour, endminute))

After initializing the end hour and minute as you did, you extract again the minutes and hours from the end minutes.

Finally, you adjust the hours to be between 0 and 23.

Uriel
  • 15,579
  • 6
  • 25
  • 46
1

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
shad0w_wa1k3r
  • 12,955
  • 8
  • 67
  • 90
1

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.

Batman
  • 8,571
  • 7
  • 41
  • 80
1

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="")
0

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)
0

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)
0
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)
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
  • Since this question already has a lot of answers, could you edit your answer to explain why you think your answer is better than the other existing answers? – Willow Feb 17 '22 at 20:43
0
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

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 17 '22 at 22:39
-1

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 :)