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

Page 1 of 1

## 1 Replies - 1374 Views - Last Post: 20 January 2013 - 01:05 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=308121&amp;s=42fa69350d5af8ee2df3cf325af85e05&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Brilliantwarrior

• New D.I.C Head

Reputation: 0
• 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

• Programming Theoretician

Reputation: 4925
• Posts: 13,317
• 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.