Calculate total time with min and sec.
-
- Posts: 88
- Joined: Fri Aug 24, 2007 8:23 pm
Calculate total time with min and sec.
Hii,
I tried to calculate the total time using the start time (min and sec are saved in chars) and the end time (min and sec stored in chars).
Now i want to calculate the time difference.
I thought about something like :
1. check if startmin > endmin :
- if 1 then : (60 - startmin) + endmin
- if 0 then : endmin - startmin
2. for seconds : i really don't know lol
Hope someone can help !
Tnx
(the max time should be 3 or 2 minutes)
I tried to calculate the total time using the start time (min and sec are saved in chars) and the end time (min and sec stored in chars).
Now i want to calculate the time difference.
I thought about something like :
1. check if startmin > endmin :
- if 1 then : (60 - startmin) + endmin
- if 0 then : endmin - startmin
2. for seconds : i really don't know lol
Hope someone can help !
Tnx
(the max time should be 3 or 2 minutes)
-
- Posts: 376
- Joined: Wed May 10, 2006 11:31 pm
I won't question what you're actually doing because I don't think I want to know...
Convert it all to seconds to get the difference.
If you want to break it down to difference in minutes and seconds, then:
Personally I wouldn't do any of the above, I always store it as seconds then convert it to minutes & seconds only when you need to display it.
Convert it all to seconds to get the difference.
Code: Select all
int starttotal = startmin + (startsec * 60);
int endtotal = endmin + (endsec * 60);
int diffsec = endtotal - starttotal;
Code: Select all
int starttotal = startmin + (startsec * 60);
int endtotal = endmin + (endsec * 60);
int diffmin = (starttotal - endtotal)/60;
int diffsec = (startotal - endtotal) % 60;
-
- Posts: 88
- Joined: Fri Aug 24, 2007 8:23 pm
I know the max time is 3 or 2 minutes. So that should work
And if i use a timer, then i need a background timer.
I only know timers with a loop(while), and i can't add it to my function.
So if anyone has a background timer, plz post.
@IWN : What if the starttime is (17):59:59 and endtime (18):02:59 ?
Then your code doesn't work..
Tnx
Cyaaa
And if i use a timer, then i need a background timer.
I only know timers with a loop(while), and i can't add it to my function.
So if anyone has a background timer, plz post.
@IWN : What if the starttime is (17):59:59 and endtime (18):02:59 ?
Then your code doesn't work..
Tnx
Cyaaa
-
- Posts: 88
- Joined: Fri Aug 24, 2007 8:23 pm
I retried and here's the code ! :
(The output is totalmin)
I only have one prob : The output is sometimes ex . 2.5604245, how can i have only 2 numbers after 2. ?
(The output is totalmin)
Code: Select all
int totalmin = 0;
if(startmin > endmin){
totalmin = (60-startmin)+endmin;
}
else if(startmin < endmin){ //Normal
totalmin = endmin-startmin;
}
int totalsec = startsec+endsec;
if(totalsec >= 60){
int secinmin = totalsec/60;
totalmin += secinmin;
}
OH, MY LORD, WHY DID YOU LEFT US?? I sometimes wonder if it's too hard for you to understand that this isn't elementary school, or if you make it on purpose to laugh while reading upset people's answers...
Even if i already know the "solution" to your HUGE problem, i tried search it on google, just to see. 7 seconds, solution found in 2nd search result for "printf formatting" ...what the @#*?^|!
This doesn't round up your value, just diplay it as you want...if you don't understand what's the difference between a binary storage and it's representations, please don't come back asking.
Even if i already know the "solution" to your HUGE problem, i tried search it on google, just to see. 7 seconds, solution found in 2nd search result for "printf formatting" ...what the @#*?^|!
Code: Select all
printf("%8.2f", value);
-
- Posts: 88
- Joined: Fri Aug 24, 2007 8:23 pm
I tried it and it doesn't work. If u looked better, u should see the code is wrong.quadrizo wrote:IMG !!
try again ...
the answer was given before by Insert_witty_name just a "if (startmin <endmin)" to add
@jean: I think you didn't understand my post. But its ok, i also found the solution, 1 min after my post.
Cyaa
you should just invert min and sec and add a if ....
try to understand before to say that is wrong ...
try to understand before to say that is wrong ...
Code: Select all
int starttotal = startsec + (startmin * 60);
int endtotal = endsec + (endmin * 60);
if(startmin>endmin)
endtotal+=60*60;
int diffmin = ( endtotal-starttotal)/60;
int diffsec = ( endtotal-starttotal) % 60;
Question dev, your code works, but what if there's only a difference of seconds ?
You have to add something like :
Cy
You have to add something like :
Code: Select all
if(startmin == endmin){
//calculate your seconds
}