1 Replies - 594 Views - Last Post: 20 January 2013 - 01:05 AM Rate Topic: -----

#1 Brilliantwarrior  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 05-December 12

Finding out new time on 24-hour clock after adding minutes.

Posted 19 January 2013 - 11:07 PM

This program takes two lines of input. The first line is a "starting time" expressed in a 24-hour clock with leading zeroes, like 08:30 or 14:07. The second line is a duration D in minutes. Print out what time it will be D minutes after the starting time. All times should be formatted as numbers between 00:00 and 23:59, but the time period may go over midnight.
strttime=input()
duration=int(input())
minutes = int(strttime[3:5])
hours=int(strttime[0:2])
minutes=minutes+duration
minutes=minutes%60


I'm supposed to find the new hours by using //60 and %24, I've tried many different solutions to get the desired result, but I'm not sure what to do from here. The final minutes will always be correct from the calculation: minutes+duration%60

Is This A Good Question/Topic? 0
  • +

Replies To: Finding out new time on 24-hour clock after adding minutes.

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4361
  • View blog
  • Posts: 12,180
  • Joined: 18-April 07

Re: Finding out new time on 24-hour clock after adding minutes.

Posted 20 January 2013 - 01:05 AM

Well we know that minutes + duration % 60 is going to give us the new minutes value. What you may not know is that if you do minutes + duration then divide it (integer division) that it is going to give you the numbers to add to the hours variable. Then we use modulus 24 to get the new number of hours.

So some pseudocode...

new minutes = (minutes + duration) % 60
hours to add = (minutes + duration) / 60
new hours = (hours + hours to add) % 24

Final time is new hours:new minutes.

Hope you get the idea now. :)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1