时差(timeIntervalSinceDate)

时间:2014-06-05 00:26:34

标签: objective-c datetime nsdate

我遇到了这段代码的麻烦,输出总是-596523小时,-14分钟。 任何想法?

NSDateFormatter *Formatter = [[NSDateFormatter alloc] init];
[Formatter setDateFormat:@"HH:mm"];
NSDate date1 = nil;
if (count == 0) {
    date1 = [NSDate date];
    _startTime.text = [Formatter stringFromDate:date1];
    [sender setTitle:@"Stop" forState:UIControlStateNormal];
}else {
    NSDate date2 = [NSDate date];
    _stopTime.text = [Formatter stringFromDate:date2];
    _startStopLabel.hidden = YES;

    long diff = (long)[date2 timeIntervalSinceDate:date1];
    diff /= 60; //Convert to minutes
    _totalTime.text = [NSString stringWithFormat:@"%d hours, %d minutes", diff/60, diff%60];
}

1 个答案:

答案 0 :(得分:0)

else分支将始终测量date2和nil之间的差异。