简体   繁体   English

经过的时间(秒,分钟,小时)

[英]Elapsed time in seconds, minutes, hours

I would like to display the elapsed time in a UILabel like this: 我想在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

I could successfully catch the elapsed time, but i think it's a different format, because it counts the seconds and minutes differently as i wanted. 我可以成功捕获经过的时间,但是我认为这是一种不同的格式,因为它按照我想要的方式计算秒和分钟。 It shows the elapsed time like this: -9 min -5 hours -10 seconds . 它显示了这样的经过时间: -9 min -5 hours -10 seconds To make my solution work i would need the second and minute format like this -120 sec , if it was 2 min ago or -120 min , if it was 2 hours ago, -48 hours , if it was two days ago. 为了使我的解决方案正常工作,如果需要2分钟前,则需要第二分钟格式,例如-120 sec ;如果是2小时前,则需要-120 min ,如果是两天前,则需要-48 hours

I think this solution could be fine, however with the wrong integers I display the minutes or seconds everytime. 我认为此解决方案可能不错,但是如果使用错误的整数,我每次都会显示分钟或秒。 I would really appreciate if someone could give me some guidence how can i fix the minute & second issue. 如果有人可以给我一些指导,我将如何解决分钟和第二个问题,我将不胜感激。 However if somebody did something like this in the past and knows a better way (or it's a bad way), i would also try it. 但是,如果有人过去做过这样的事情,并且知道更好的方法(或者这是一种不好的方法),我也会尝试一下。

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;

}

Based on your description, each value, second, minute and hour are negative. 根据您的描述,每个值,秒,分钟和小时均为负数。 This means that your comparisons are incorrect. 这意味着您的比较是不正确的。 In other words, you should either use absolute value in the if statements, or you should change your if statements to match your expected values. 换句话说,您应该在if语句中使用绝对值,或者应该更改if语句以匹配您的期望值。

Edited: Your code may be easier to handle if you change your duration to be positive, then simply place a negative sign in the formatted result. 编辑:如果将持续时间更改为正数,然后在格式化结果中简单地放置一个负号,则代码可能会更易于处理。 In other words, if conversionInfo is always negative, then switch date1 and date2. 换句话说,如果conversionInfo始终为负,则切换date1和date2。 Then when formatting the string, add a minus sign in front of it. 然后,在格式化字符串时,请在其前面添加一个减号。

NSDateComponent will always divide you the date into these components. NSDateComponent将始终将日期划分为这些部分。 So think of it like you have the Date 31.3.2014 15:08:12 then NSDateComponent will deliver you: 因此,以为您拥有日期为31.3.2014 15:08:12,那么NSDateComponent将为您提供:

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

In case you have a difference these values might be negative, but [NSDateComponent second] for example will still just be the seconds part of the time difference and no the whole time difference in seconds format. 如果您有差异,则这些值可能为负,但是例如[NSDateComponent second]仍将只是时差的秒部分,而不会是秒格式的整个时差。 So if I have a time difference of -4 minutes and -3 seconds, [NSDateComponent second] will be -3 and [NSDateComponent minute] will be -4. 因此,如果我的时差为-4分钟和-3秒,则[NSDateComponent second]为-3, [NSDateComponent minute]为-4。

So in case you want your own description of elapsed time you could use the timeIntervalSince1970 method of NSDate and compare the UNIX Timestamps directly and calculate the wanted formats, or you use NSDateComponent and check against the rules.. So if there is no hour but x minutes.. your elapsed time in seconds is: numberOfMinutes * 60 + numberOfSeconds where numberOfminutes is your [NSDateComponent minute] and numberOfSeconds is your [NSDateComponent second] 因此,如果您想要自己的经过时间描述,可以使用NSDate的timeIntervalSince1970方法并直接比较UNIX时间戳并计算所需的格式,或者使用NSDateComponent并对照规则进行检查。 minutes ..您经过的时间(以秒为单位)是: numberOfMinutes * 60 + numberOfSeconds ,其中numberOfminutes是您的[NSDateComponent minute] ,numberOfSeconds是您的[NSDateComponent second]

So I would recommend you check in the following pseudo code order: 因此,我建议您按以下伪代码顺序进行检查:

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

and so on.. 等等..

//Disclaimer: no guarantee on the correctness of the provided sample calculation //免责声明:不保证所提供样本计算的正确性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM