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!
You need to use an NSCalendar
to extract that date's NSDateComponents
.
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.
精彩评论