【发布时间】:2012-12-24 10:13:21
【问题描述】:
我已经阅读了一些关于如何在 iOS 中计算 2 个日期之间的差异的线程,这是一个似乎也由 Apple 文档提供的示例,我用它来确定 2 个日期是否相同(忽略时间) .但是组件:方法总是返回年=0、月=0、日=0,即使两个日期不同。我不知道为什么......我会很感激你的想法......
+ (BOOL)isSameDate:(NSDate*)d1 as:(NSDate*)d2 {
if (d1 == d2) return true;
if (d1 == nil || d2 == nil) return false;
NSCalendar* currCal = [NSCalendar currentCalendar];
// messing with the timezone - can also be removed, no effect whatsoever:
NSTimeZone* tz = [NSTimeZone timeZoneForSecondsFromGMT:0];
[currCal setTimeZone:tz];
NSDateComponents* diffDateComps =
[currCal components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit
fromDate:d1 toDate:d2 options:0];
return ([diffDateComps year] == 0 && [diffDateComps month] == 0 && [diffDateComps day] == 0);
}
【问题讨论】:
-
尝试使用此处提到的方法比较日期,stackoverflow.com/questions/13301980/…。在那里检查答案。
标签: ios nscalendar nsdatecomponents