# Help in calculating time different!

Page 1 of 1

## 7 Replies - 1605 Views - Last Post: 11 October 2010 - 08:51 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=194453&amp;s=c4f2c89f129215cc0b6c531f85a69e1e&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 ROCKY86

Reputation: 0
• Posts: 47
• Joined: 05-August 07

# Help in calculating time different!

Posted 11 October 2010 - 05:29 AM

hi guys! i need help on calculating about the time differences in 24hour format!! A program that reads in start time, end time then output the time difference!!

example: Enter Start time: 2355
Enter End time : 0005
Time difference is 10min

I have no idea how to do that!! I understand I can take end time to minus the start time, however that don't give me the time differences!!

Is This A Good Question/Topic? 0

## Replies To: Help in calculating time different!

### #2 aniri

Reputation: 54
• Posts: 657
• Joined: 24-November 09

## Re: Help in calculating time different!

Posted 11 October 2010 - 05:44 AM

This site has a policy against just handing people the code they need. Show us what you've tried so far and we'll help

### #3 ROCKY86

Reputation: 0
• Posts: 47
• Joined: 05-August 07

## Re: Help in calculating time different!

Posted 11 October 2010 - 06:08 AM

sure!! this is my coding!
```int start=0, end=0, hour=0, diffmin=0,min=2360;

System.out.print("Enter starting time: ");
start = sc.nextInt();

while(start!=-1)
{

System.out.print("Enter ending time: ");
end = sc.nextInt();

if(end>start)
{
diffmin = end - start;

}
else if(end<start)
{
end+=min;
diffmin=end-start;
}
System.out.println("Time difference is "+diffmin+ " min");
start=0;//reset to 0
end=0;//reset to 0
System.out.print("Enter starting time: ");
start = sc.nextInt();

}

System.out.print("<Program terminates>");

}

}

```

basically what my program do is ask for user input! after that it print out the time differences between start and end! if the user enter -1 it will terminate the program! however! if user enter

Start time:2311
end Time: 2321
Time difference is 10min

This output is correct! BUT

Start time:0021
end Time: 0011
Time difference is 2350min

that wrong!THE time difference should be 1430min but I got no idea how to calculate to make it !

This post has been edited by ROCKY86: 11 October 2010 - 07:14 AM

### #4 Agentsnakezero

Reputation: 4
• Posts: 34
• Joined: 28-September 10

## Re: Help in calculating time different!

Posted 11 October 2010 - 06:16 AM

Since you are using military time you should check and see if end time is less than start time and if it is then add 2400 (to simulate the next day) and then subtract it

### #5 ROCKY86

Reputation: 0
• Posts: 47
• Joined: 05-August 07

## Re: Help in calculating time different!

Posted 11 October 2010 - 06:32 AM

Agentsnakezero, on 11 October 2010 - 05:16 AM, said:

Since you are using military time you should check and see if end time is less than start time and if it is then add 2400 (to simulate the next day) and then subtract it

I did that!!, if my end time is less then starting time, I add to 2359 then subtract the start time! however the condition is first two digits fall between 00 to 23 hour while its last two digits fall betwwen 00 to 59min!!

### #6 Agentsnakezero

Reputation: 4
• Posts: 34
• Joined: 28-September 10

## Re: Help in calculating time different!

Posted 11 October 2010 - 07:18 AM

thats why you add 2400(thats the value of one day not 2359), and actually it doesnt matter what you have after you add the number because it subtractive and you always will get a normalized minuted or another way to test for everything is (time%100<=59) gives you minutes that are correct and (time/100<=24) will check hours. and physically if you wanted to using this
```
int min1=(start%100+(start/100*60));
int min2=(end%100+(end/100*60));
if(min2<min1){
min2+1440;
total=min2-min1;}
else
total=min2-min1;

```
that should work
and then if your second time after the

This post has been edited by Agentsnakezero: 11 October 2010 - 07:22 AM

### #7 ROCKY86

Reputation: 0
• Posts: 47
• Joined: 05-August 07

## Re: Help in calculating time different!

Posted 11 October 2010 - 07:41 AM

Agentsnakezero, on 11 October 2010 - 06:18 AM, said:

thats why you add 2400(thats the value of one day not 2359), and actually it doesnt matter what you have after you add the number because it subtractive and you always will get a normalized minuted or another way to test for everything is (time%100<=59) gives you minutes that are correct and (time/100<=24) will check hours. and physically if you wanted to using this
```
int min1=(start%100+(start/100*60));
int min2=(end%100+(end/100*60));
if(min2<min1){
min2+1440;
total=min2-min1;}
else
total=min2-min1;

```
that should work
and then if your second time after the

thx for help! but the code look messy!! and hard to understand expecially this 2 statement:
int min1=(start%100+(start/100*60));
int min2=(end%100+(end/100*60));

is there any simply code to do it??

### #8 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11345
• Posts: 42,784
• Joined: 27-December 08

## Re: Help in calculating time different!

Posted 11 October 2010 - 08:51 AM

It looks fine to me. You can break up the math into multiple statements if you want, but I see nothing wrong with what you have.

What it does is take the remainder of start/100, and adds it to (start/100 * 60).