我正在尝试检查两个日期之间的时间是否大于 3 个月,但没有成功。这是我正在尝试的。我做错了什么吗?即使我在昨天设置了detailTextDate,它仍然会显示evaluationLabel。我需要设置日期格式吗?
NSString *detailTextDate = @"August 11, 2013";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateStyle = NSDateFormatterLongStyle;
NSDate *startDate = [dateFormatter dateFromString:detailTextDate];
[dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
NSDate *todaysDate = [NSDate date];
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [gregorianCalendar components:NSMonthCalendarUnit
fromDate:startDate
toDate:todaysDate
options:0];
if(components.month >= 3){
evaluationLabel.hidden = NO;
}
}
//tried using NSYearCalendarUnit, Month, Day etc.
//startDate = 2013-08-11 06:00:00 +0000, detailTextDate = August 11, 2013, components = //<NSDateComponents: 0x176c3c70>
// Month: 0