开发者社区> 问答> 正文

关于iOS 计算起始时间和终止时间的差

简单的就是说 a = 终止 - 起始
求大神 帮忙解答,
求代码演示;

我是这样算的

(void)viewDidAppear:(BOOL)animated
 {

NSDateFormatter *formatter =[[NSDateFormatter alloc] init];
 [formatter setDateFormat:@"yyyy-MM-dd-HH-mm-ss"];
 self.currentTime = [formatter stringFromDate:[NSDate date]];
 NSLog(@"DATE = %@", [NSDate date]);

NSLog(@"ooooo%@", self.currentTime);


}
(void)viewWillDisappear:(BOOL)animated { NSDateFormatter *date=[[NSDateFormatter alloc] init]; [date setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; NSString *currentEndTime = [date stringFromDate:[NSDate date]]; NSCalendar *cal=[NSCalendar currentCalendar]; unsigned int unitFlags= NSYearCalendarUnit| NSMonthCalendarUnit| NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit; NSDateComponents *d = [cal components:unitFlags fromDate:self.currentTime toDate:currentEndTime options:0]; NSLog(@"%d天%d小时%d分钟%d秒",[d day],[d hour],[d minute],[d second]);

}

打印

015-04-27 13:47:36.082 H_J_4[30420:576015] -[__NSCFString timeIntervalSinceReferenceDate]: unrecognized selector sent to instance 0x7fd6c144ebd0
 (lldb) 

展开
收起
爵霸 2016-03-13 10:06:02 2608 0
1 条回答
写回答
取消 提交回答
  • ios计算时间差 转

     //模拟windows下 GetTickCount //
     //#import
    
    (int)mxGetTickCount;
     {
     int timeTick;
     mach_timebase_info_data_t info;
     uint64_t machineTime;
     mach_timebase_info(&info);
     machineTime =mach_absolute_time();
     timeTick = machineTime * info.numer / info.denom /1000000LL;
    
    return timeTick;
     }
    
    
    // NSDate timeIntervalSinceDate:
    
    //下面转换NSString,用在保存时间时使用。
    (double)GetStringTimeDiff:(NSString*)timeS timeE:(NSString*)timeE
    
    {
    double timeDiff = 0.0;
    
    NSDateFormatter *formatters = [[NSDateFormatter alloc] init];
    [formatters setDateFormat:@"yyyy/MM/dd HH:mm:ss:SSS"];
    NSDate *dateS = [formatters dateFromString:timeS];
    
    
    
    NSDateFormatter *formatterE = [[NSDateFormatter alloc] init];
    [formatterE setDateFormat:@"yyyy/MM/dd HH:mm:ss:SSS"];
    NSDate *dateE = [formatterE dateFromString:timeE];
    
    
    timeDiff = [dateE timeIntervalSinceDate:dateS ];
    
    return timeDiff;
    
    
    }
    (NSString*)GetCurTime
    
    {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    [formatter setDateFormat:@"yyyy/MM/dd HH:mm:ss:SSS"];
    
    NSString*timeString=[formatter stringFromDate: [NSDate date]];
    
    [formatter release];
    
    return timeString;
    
    
    }
    2019-07-17 19:01:56
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
手淘iOS性能优化探索 立即下载
From Java/Android to Swift iOS 立即下载
深入剖析iOS性能优化 立即下载