iOS计算两个时间差
//获取两个date直接的时间差
-
(void)getBabyDetailAge:(long long )time
{
NSDate *date1 = [NSDate dateWithTimeIntervalSince1970:time];
NSDate *date2 = [NSDate date];
NSCalendar *gregorian = [[ NSCalendar alloc ] initWithCalendarIdentifier : NSCalendarIdentifierGregorian];
NSUInteger unitFlags = NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
NSDateComponents *components = [gregorian components:unitFlags fromDate:date1 toDate:date2 options:0];NSInteger years = [components year];
NSInteger months = [components month];
NSInteger days = [components day];
NSInteger hour = [components hour];
NSInteger minute = [components minute];
NSInteger second = [components second];
NSLog(@“时间差为 %@-%@-%@ %@:%@:%@”,@(years),@(months),@(days),@(hour),@(minute),@(second));
}
分别可以计算出两个时间间隔多少年、月、日、时、分、秒。