display the difference in Days, Hours, Minutes and

2019-09-07 07:10发布

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条回答
小情绪 Triste *
2楼-- · 2019-09-07 07:37

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

查看更多
登录 后发表回答