计算两个日期之间相差多少天

1.不是以每24小时记为一天,当过了午夜12点就是一天

- (NSInteger)getDaysFrom:(NSString*)lastTime {

NSCalendar* gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

NSDate* fromdate = [NSDate dateWithTimeIntervalSince1970:[lastTime doubleValue] / 1000.0];

NSDate* date = [NSDate date];

[gregorian setFirstWeekday:2];

NSDate *fromDate;

NSDate *toDate;

[gregorian rangeOfUnit:NSCalendarUnitDay startDate:&fromDate interval:NULL forDate:fromdate];

[gregorian rangeOfUnit:NSCalendarUnitDay startDate:&toDate interval:NULL forDate:date];

NSDateComponents *dayComponents = [gregorian components:NSCalendarUnitDay fromDate:fromDate toDate:toDate options:0];

return dayComponents.day;

}

你可能感兴趣的:(计算两个日期之间相差多少天)