我正在尝试建立一个倒计时,显示秒数。使用NSDateComponents准确计时
以下示例设置为10秒,但输出为8秒。
NSDate *currentDate = [NSDate date];
NSDate *finishDate = [NSDate dateWithTimeInterval:10 sinceDate:currentDate];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [calendar components:NSSecondCalendarUnit|NSMinuteCalendarUnit|NSHourCalendarUnit | NSDayCalendarUnit| NSMonthCalendarUnit | NSYearCalendarUnit
fromDate:currentDate
toDate:finishDate
options:0];
NSInteger hour_ = [components hour];
NSInteger minute_ = [components minute];
NSInteger second_ = [components second];
登录
currentDate = 2012-06-05 03:13:10 +0000
finishDate_ = 2012-06-05 03:13:19 +0000
second_ = 8
请注意,这不是真正的“时间”,但更多的日期计算...... – lnafziger