display the difference in Days, Hours, Minutes and Seconds with it then updating in realtime?

放肆的年华 提交于 2019-12-24 22:32:21

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!