计算并显示两个 NSDate 之间的时间

Posted

技术标签:

【中文标题】计算并显示两个 NSDate 之间的时间【英文标题】:Calculate and display time between two NSDates 【发布时间】:2015-08-14 13:13:31 【问题描述】:

最初的目标是使用 NSDateComponents 计算两个 NSDate 之间的差异。此外,一旦获得并验证正确的天、小时、分钟和秒。在 UITableViewCell 中显示具有差异(到最接近的分钟、小时或天)的实时代码(计数器)。 目前的进展可以在下面找到。 任何建议或完成此任务的方向将不胜感激!

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

  static NSString *CellIdentifier =@"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.textLabel.text = [(AGSGraphic *)[_radios objectAtIndex:indexPath.row] attributeForKey:@"parent_id"];

NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"YYYY-MM-dd hh:mm:ss"];

NSNumber *timestamp = [[_radios objectAtIndex:indexPath.row] valueForKey:@"date_time_edt"];
NSDate *today = [NSDate date];

NSDate *crewDate = [NSDate dateWithTimeIntervalSince1970:[timestamp doubleValue]/1000];
NSString *temp = [df stringFromDate:today];
NSString *crewtemp = [df stringFromDate:crewDate];
crewDate = [df dateFromString:crewtemp];
today = [df dateFromString:temp];
NSLog(@"%@", crewDate);
NSLog(@"%@", today);
NSTimeInterval interval = [crewDate timeIntervalSinceDate:today];
interval = -interval;

NSInteger minutes = interval/60;
NSInteger hours = minutes/60;
NSInteger days = hours/24;
//    NSInteger weeks = days/7;
//    NSInteger months = weeks/4;
//    NSInteger years = months/12;


//    NSTimeInterval years = interval/31536000;
//    NSTimeInterval months = years/12;
//    NSTimeInterval weeks = years/52;
//    NSTimeInterval days = years/365;
//    NSTimeInterval hours = days/24;
//    NSTimeInterval minutes = hours/60;

//    NSLog(@"%f", interval);

if(minutes < 60)

    cell.detailTextLabel.text = [NSString stringWithFormat:@"%ld minuntes ago", (long)minutes];

else if(hours < 24)

    cell.detailTextLabel.text = [NSString stringWithFormat:@"%ld hours ago", (long)hours];

else

    cell.detailTextLabel.text = [NSString stringWithFormat:@"%ld days ago", (long)days];


return cell;

【问题讨论】:

NSDateIntervalFormatter 你能举一两个例子来说明你希望最终字符串的样子吗? YYYY-MM-dd hh:mm:ss,从这里我会将组件传递到一个方法中,该方法将在屏幕上打印差异(四舍五入到最接近的分钟、小时或天) 四舍五入到最接近的小时、分钟或天,但您仍想使用包含秒的格式字符串(ss)? 【参考方案1】:

这是使用 Minutes 的方法:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

    NSHLessonCell *cell = [tableView dequeueReusableCellWithIdentifier:NSHLessonCellIdentifier forIndexPath:indexPath];

    NSDate *fromDate;
    NSDate *toDate;

    NSCalendar *calendar = [NSCalendar currentCalendar];

    [calendar rangeOfUnit:NSCalendarUnitMinute startDate:&fromDate interval:NULL forDate:[self.ninjas[indexPath.row] started]];    
    [calendar rangeOfUnit:NSCalendarUnitMinute startDate:&toDate interval:NULL forDate:[self.ninjas[indexPath.row] finished]];
    NSDateComponents *difference = [calendar components:NSCalendarUnitMinute fromDate:fromDate toDate:toDate options:0];

    [cell setTotalDuration:[difference minute]];

另外,我只假设使用我使用的分钟方法,您还可以使用其他方法来计算不同日期测量单位的差异,方法是用 NSCalendar.h 中提供的选项替换 NSCalendarUnitMinute,见下文:

typedef NS_OPTIONS(NSUInteger, NSCalendarUnit) 
        NSCalendarUnitEra                = kCFCalendarUnitEra,
        NSCalendarUnitYear               = kCFCalendarUnitYear,
        NSCalendarUnitMonth              = kCFCalendarUnitMonth,
        NSCalendarUnitDay                = kCFCalendarUnitDay,
        NSCalendarUnitHour               = kCFCalendarUnitHour,
        NSCalendarUnitMinute             = kCFCalendarUnitMinute,
        NSCalendarUnitSecond             = kCFCalendarUnitSecond,
        NSCalendarUnitWeekday            = kCFCalendarUnitWeekday,
        NSCalendarUnitWeekdayOrdinal     = kCFCalendarUnitWeekdayOrdinal,
        NSCalendarUnitQuarter            NS_ENUM_AVAILABLE(10_6, 4_0) = kCFCalendarUnitQuarter,
        NSCalendarUnitWeekOfMonth        NS_ENUM_AVAILABLE(10_7, 5_0) = kCFCalendarUnitWeekOfMonth,
        NSCalendarUnitWeekOfYear         NS_ENUM_AVAILABLE(10_7, 5_0) = kCFCalendarUnitWeekOfYear,
        NSCalendarUnitYearForWeekOfYear  NS_ENUM_AVAILABLE(10_7, 5_0) = kCFCalendarUnitYearForWeekOfYear,
        NSCalendarUnitNanosecond         NS_ENUM_AVAILABLE(10_7, 5_0) = (1 << 15),
        NSCalendarUnitCalendar           NS_ENUM_AVAILABLE(10_7, 4_0) = (1 << 20),
        NSCalendarUnitTimeZone           NS_ENUM_AVAILABLE(10_7, 4_0) = (1 << 21),

【讨论】:

如果您不介意详细说明“开始/完成”的由来,将不胜感激! 好的,刚刚意识到那些是 BOOL(s) 是的,我实际上已经实现了目标。我将更新原始代码!感谢您的帮助和礼貌跟进。 @Larcerax

以上是关于计算并显示两个 NSDate 之间的时间的主要内容,如果未能解决你的问题,请参考以下文章

在两个NSDate之间获得准确的时间差?

计算两个日期之间的天数

计算两个日期的天数问题

如何计算设备断电的剩余时间

从两个 NSDate 计算完整经过的友好时间

计算两个时间之间的差值