直接上代码:
注:代码中的model.recentTime就是你自己要计算的时间
NSString * newTime = [model.recentTime substringToIndex:19];//取特定的字符串与系统给的格式匹配
NSLog(@"newTime%@",newTime);
//dateAnd日历
NSTimeZone *zone = [NSTimeZone systemTimeZone];
NSDate *startDate = [NSDate date];//起始时间
NSInteger interval = [zone secondsFromGMTForDate: startDate];
NSDate *localDate = [startDate dateByAddingTimeInterval: interval];
NSLog(@"startDate%@",localDate);
//字符窜转日期格式
NSDateFormatter * inputStr =[[NSDateFormatter alloc] init];
[inputStr setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];//美国制
[inputStr setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *endDate = [inputStr dateFromString:newTime];//终止时间
NSInteger interVal2 = [zone secondsFromGMTForDate:endDate];
NSDate * localEndDate = [endDate dateByAddingTimeInterval:interVal2];
NSLog(@"endDate%@",localEndDate);
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSUInteger unitFlags = NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit;
NSDateComponents *components = [gregorian components:unitFlags fromDate:localDate toDate:localEndDate options:0];
NSInteger days = [components day];
NSInteger hours = [components hour];
NSInteger minutes = [components minute];
// NSLog(@"months:%lddays:%ld:hours%ld,minute:%ld",(long)months,(long)days,(long)hours,(long)minute);
self.DiseaseNameLabel.text = model.reminderTitle;
self.distanceTimeLabel.text =[NSString stringWithFormat:@"倒计时:%ld天%ld时%ld分",(long)days,(long)hours,(long)minutes];
self.contentLabel.text = model.medicationNames;
//拼接参数
NSTimeInterval second = days*24*60*60+hours*60*60+minutes*60;