两个时间差

NSDate *nowDate = [NSDate date];

NSDateFormatter *dateFomatter = [[NSDateFormatter alloc] init];

dateFomatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";

// 截止时间字符串格式

NSString *expireDateStr = @"2017-04-22 15:33:57";

// 当前时间字符串格式

NSString *nowDateStr = [dateFomatter stringFromDate:nowDate];

// 截止时间data格式

NSDate *expireDate = [dateFomatter dateFromString:expireDateStr];

// 当前时间data格式

nowDate = [dateFomatter dateFromString:nowDateStr];

// 当前日历

NSCalendar *calendar = [NSCalendar currentCalendar];

// 需要对比的时间数据

NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth

| NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;

// 对比时间差

NSDateComponents *dateCom = [calendar components:unit fromDate:expireDate toDate:nowDate options:0];

// 差值

NSLog(@"年差额 = %ld, 月差额 = %ld, 日差额 = %ld, 小时差额 = %ld, 分钟差额 = %ld, 秒差额 = %ld", (long)dateCom.year,(long)dateCom.month,(long)dateCom.day,(long)dateCom.hour,(long)dateCom.minute,(long)dateCom.second);

你可能感兴趣的:(两个时间差)