如果我打电话
NSDateComponents * components = [calendar components:(NSMonthCalendarUnit | NSYearCalendarUnit) fromDate:self];
而不是问,让我们说
components.minute
它会保证给我 0 吗?
我做了 像这样,但是为了确保它 100% 正常工作,上述条件也需要针对 iOS 的下一个版本进行处理
- (BOOL)isInTheSameComponents:(NSUInteger)components withDate:(NSDate *)date {
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components1 = [calendar components:components fromDate:self];
NSDateComponents *components2 = [calendar components:components fromDate:date];
return ((components1.month == components2.month) && (components1.year == components2.year) && (components1.day == components2.day) && (components1.hour == components2.hour) && (components1.minute == components2.minute));
}
- (BOOL)isInTheSameMonthAndYearWithDate:(NSDate *)date {
return [self isInTheSameComponents:(NSMonthCalendarUnit | NSYearCalendarUnit) withDate:date];
}