PDA

View Full Version : How to get "Total Minutes" (TIMER)

Nekbeth
Apr 24, 2011, 12:43 PM
Hello, I'm trying to get the Total minutes for a Timer. I already got the seconds working. Here is how I got "Total Seconds":

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSDateComponents *comps = [gregorian components:(NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:time];

NSInteger hour = [comps hour];

NSInteger minute = [comps minute];

NSLog(@"Hour:%i", hour);

NSLog(@"minute:%i", minute);

NSInteger secs =hour * 60 * 60 + minute * 60;

NSNumber *elapsedSeconds = [[NSNumber alloc] initWithInt:secs];

NSDictionary *myDict = [NSDictionary dictionaryWithObject:elapsedSeconds forKey:@"TotalSeconds"];

It might be only to change the formula, any help would be much appreciated. Thanks

robbieduncan
Apr 24, 2011, 12:46 PM
This may be massively simplistic but if you know the total elapsed seconds what is preventing you dividing that by 60 to get the number of minutes?

Nekbeth
Apr 24, 2011, 01:00 PM
You mean, something like :

NSInteger mins = secs / 60;
NSNumber *elapsedMinutes = [[NSNumber alloc] initWithInt:mins];

NSDictionary *myMin = [NSDictionary dictionaryWithObject:elapsedMinutes forKey:@"TotalMinutes"]; ??

robbieduncan
Apr 24, 2011, 01:10 PM
Assuming you only want the number of complete minutes then yes.

Nekbeth
Apr 24, 2011, 01:21 PM
Great, Thanks Duncan :D

robbieduncan
Apr 24, 2011, 01:22 PM
Great, Thanks Duncan :D

What makes you think my name is Duncan?

Nekbeth
Apr 24, 2011, 07:52 PM
I guess because of your nickname: robbieduncan

robbieduncan
Apr 25, 2011, 02:58 AM
I guess because of your nickname: robbieduncan

Yes. And Duncan is my surname. So it's weird to be referred to by it

Nekbeth
Apr 25, 2011, 09:11 AM
:) , is robbie ok ? * while you see this message, do you know how to make "Total Second" to reach 60 and loop again, instead of showing me "456 seconds remaing". *Minutes change if I set the scheduletime for 60, but only show up in my log until the first minute has passed. *

(I'm using a date picker)

Thanks

robbieduncan
Apr 26, 2011, 05:03 AM
:) , is robbie ok ?

Yes :)

* while you see this message, do you know how to make "Total Second" to reach 60 and loop again, instead of showing me "456 seconds remaing". *Minutes change if I set the scheduletime for 60, but only show up in my log until the first minute has passed. *

(I'm using a date picker)
Thanks

Look at the modulus operator (%). For example 456 % 60 is 36.