问题
I am a noob... I have
QuitTime.text = quitDate;
Which is a date in the past... How can I display the difference in Days, Hours, Minutes and Seconds with it then updating in realtime?
回答1:
You can use NSDateComponents
NSDate *currentDate = [NSDate date];
NSCalendar *gregorian = [[[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar]autorelease];
NSUInteger unitFlags = NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *components = [gregorian components:unitFlags
fromDate:quitDate
toDate:currentDate options:0];
NSLog(@"Component Month:%d",[component month]);
NSLog(@"Component Day:%d",[component day]);
NSLog(@"Component Hour:%d",[component hour]);
NSLog(@"Component Mins:%d",[component minute]);
Reference:Difference between two NSDate objects -- Result also a NSDate
来源:https://stackoverflow.com/questions/13335281/display-the-difference-in-days-hours-minutes-and-seconds-with-it-then-updating