iOS 两日期之间相差几天
2015-12-01 本文已影响4459人
劉光軍_MVP
- (NSInteger)getDifferenceByDate:(NSString *)date {
//获得当前时间
NSDate *now = [NSDate date];
//实例化一个NSDateFormatter对象
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//设定时间格式
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *oldDate = [dateFormatter dateFromString:date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
unsigned int unitFlags = NSDayCalendarUnit;
NSDateComponents *comps = [gregorian components:unitFlags fromDate:oldDate toDate:now options:0];
return [comps day];
}