用它来显示天,小时,分钟和秒的差异,然后实时更新?

时间:2012-11-11 20:45:28

标签: iphone ios date timer

我是个菜鸟...... 我有

QuitTime.text = quitDate;

这是过去的日期...... 如何用天数,小时,分钟和秒显示差异,然后实时更新?

1 个答案:

答案 0 :(得分:0)

您可以使用 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]);

参考:Difference between two NSDate objects -- Result also a NSDate