经过的时间以秒,分钟,小时为单位

时间:2014-07-06 20:54:21

标签: ios objective-c nsdate

我想以UILabel显示经过的时间,如下所示:

 x sec | when the elapsed time is less than a minute compared to now 
 y minutes | when the elapsed time is less than a hour, but greater than 60 sec compared to now
 z hours | when the elapsed time is greater than 60 sec and 60 minutes, but less than 24 hours +1 seconds compared to now

我可以成功捕捉经过的时间,但我认为这是一种不同的格式,因为它按照我想要的方式计算秒数和分钟数。它显示了这样的经过时间:-9 min -5 hours -10 seconds。要使我的解决方案正常工作,我需要第二和分钟格式,如-120 sec,如果是2分钟前或-120 min,如果是2小时前,-48 hours,如果是两天前。

我认为这个解决方案可能没问题,但是错误的整数我每次都会显示分钟或秒。我真的很感激,如果有人能给我一些指导,我该如何解决这个问题?第二期。然而,如果有人在过去做过这样的事情并且知道更好的方式(或者这是一种糟糕的方式),我也会尝试一下。

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MM/dd/yy HH:mm:ss"];

NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:self.fromDate];

NSTimeInterval timeInterval = [dateFromString timeIntervalSinceNow];

NSCalendar *deviceCalendar = [NSCalendar currentCalendar];


NSDate *date1 = [[NSDate alloc] init];
NSDate *date2 = [[NSDate alloc] initWithTimeInterval:timeInterval sinceDate:date1];

unsigned int unitFlags = NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;


NSDateComponents *conversionInfo = [deviceCalendar components:unitFlags fromDate:date1  toDate:date2  options:0];

NSLog(@" %d min %d hours %d seconds",[conversionInfo minute], [conversionInfo hour], [conversionInfo second]);

if ([conversionInfo second] < 60 || [conversionInfo minute] < 0 || [conversionInfo hour] < 0){

    NSString *secondString = [NSString stringWithFormat:@"%d",[conversionInfo second]];
    cell.time.text = secondString;

}

if ([conversionInfo second] > 60 || [conversionInfo minute] <= 60  || [conversionInfo hour] < 0){

    NSString *minuteString = [NSString stringWithFormat:@"%d",[conversionInfo minute]];
    cell.time.text = minuteString;

}

if ([conversionInfo second] > 60 || [conversionInfo minute] > 60 || [conversionInfo hour] >= 1){

    NSString *hourString = [NSString stringWithFormat:@"%d",[conversionInfo hour]];
    cell.time.text = hourString;

}

2 个答案:

答案 0 :(得分:1)

根据您的描述,每个值,秒,分钟和小时都是负数。 这意味着您的比较不正确。 换句话说,您应该在if语句中使用绝对值,或者您应该更改if语句以匹配您的预期值。

编辑: 如果您将持续时间更改为正数,则可能更容易处理您的代码,然后只需在格式化结果中放置一个负号。 换句话说,如果conversionInfo始终为负数,则切换date1和date2。 然后在格式化字符串时,在它前面添加一个减号。

答案 1 :(得分:1)

NSDateComponent将始终将日期划分为这些组件。所以想想就像你有日期31.3.2014 15:08:12然后NSDateComponent会传达给你:

 day: 31
 month: 3
 year: 2014
 hours: 15
 minutes:8
 seconds:12

如果你有差异,这些值可能是负数,但例如[NSDateComponent second]仍然只是时差的秒部分,而不是以秒为单位的整个时间差。因此,如果我有-4分钟和-3秒的时差,[NSDateComponent second]将为-3而[NSDateComponent minute]将为-4。

因此,如果您想要自己描述已用时间,可以使用NSDate的timeIntervalSince1970方法并直接比较UNIX时间戳并计算所需格式,或者使用NSDateComponent并检查规则..所以,如果没有小时但是x分钟..你的经过时间以秒为单位:numberOfMinutes * 60 + numberOfSeconds其中numberOfminutes是你的[NSDateComponent minute]而numberOfSeconds是你的[NSDateComponent second]

所以我建议您检查以下伪代码顺序:

if (days > 0) {
   elapsed = hour + minutes/60 + seconds / 3600;
} else if (hour > 0) {
   elapsed = hour*60 + minutes + seconds / 60;
} else if (minute>0) {

依旧......

//免责声明:无法保证所提供的样本计算的正确性