麻烦总时间

时间:2014-06-03 12:46:07

标签: objective-c nsdateformatter

需要帮助。 我已经看到了其他答案,但对此并不了解。如何以小时和分钟打印开始和停止时间之间的时差?

NSDateFormatter *Formatter = [[NSDateFormatter alloc] init];
[Formatter setDateFormat:@"HH:mm"];
if (count == 0) {
    _startTime.text = [Formatter stringFromDate:[NSDate date]];
    [sender setTitle:@"Stop" forState:UIControlStateNormal];
}else {
    _stopTime.text = [Formatter stringFromDate:[NSDate date]];
    _startStopLabel.hidden = YES;
    //write to UILabel "totalTime" the difference between the stop and start times in hours, minutes.
}

如果需要什么,请问:)(第一篇文章) 谢谢大家:))

1 个答案:

答案 0 :(得分:0)

您需要减去日期,然后转换生成的时间间隔:

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
    //write to UILabel "totalTime" the difference between the stop and start times in hours, minutes.
    _totalTime.text = [NSString stringWithFormat:@"%d hours, %d minutes", diff/60, diff%60];
}