首页 文章

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

提问于
浏览
0

最初的目标是使用NSDateComponents计算两个NSDates之间的差异 . 此外,一旦获得日期,小时,分钟和秒,并验证为正确 . 显示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;
}

1 回答

  • 0

    这是你如何使用分钟来做到这一点:

    - (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),
    

相关问题