Reputation: 11
I want to calculate the numbers of hours passed on a day from an NSDate object (in UT). Basically, the example should look something like "17 hours and 31 minutes".
Thanks!
Upvotes: 1
Views: 667
Reputation: 19143
Have a look at Erica Sadun's NSDate utilities. You can either use them in your project or just get some inspiration on how to solve your problem.
But to answer your particular question, this is pretty easy, since NSDates are basically just counting seconds since a reference time. This works:
NSTimeInterval interval = [[NSDate dateWithTimeIntervalSinceNow:0] timeIntervalSinceDate:[NSDate dateWithTimeIntervalSinceReferenceDate:0]];
interval = interval / 3600.0f;
float hours;
float fraction;
fraction = modff(interval, &hours);
int minutes = (int)(fraction * 100.0f);
NSLog(@"%d hours and %d minutes", (int)hours, (int)minutes);
Maybe you can get rid of some of the casts.
Upvotes: 1
Reputation: 243156
You need to use an NSCalendar
to extract that date's NSDateComponents
.
Upvotes: 2