如果你找了好久时间差还没计算对,那恭喜你能看到这篇文章,喜欢的亲点赞
第一步:初始化日历
NSCalendar *gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
第二步:设置区域和时区
NSLocale * locale = [NSLocale autoupdatingCurrentLocale];
[gregorian setLocale:locale];
NSTimeZone * timeZone = [NSTimeZone systemTimeZone];
[gregorian setTimeZone:timeZone];
第三步:格式化时间
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
第四步:计算天数
NSDate *fromDate;
NSDate *toDate;
[gregorian rangeOfUnit:NSCalendarUnitDay startDate:&fromDate interval:NULL forDate:[dateFormatter dateFromString:fromDay]];
[gregorian rangeOfUnit:NSCalendarUnitDay startDate:&toDate interval:NULL forDate:[dateFormatter dateFromString:toDay]];
第五步:比较天数
NSDateComponents *dayComponents = [gregorian components:NSCalendarUnitDay fromDate:fromDate toDate:toDate options:0];
此处只给出部分代码,需要的话请留言