iphone – 比较两个NSDates相同的日期/时间

参见英文答案 > How to compare two dates in Objective-C                                    14个
是date1 == date2不是有效的比较方式吗?如果没有,什么是正确的选择?

这是我的代码:

- (NSDate*) dateWithNoTime {
    unsigned int flags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
    NSCalendar* calendar = [NSCalendar currentCalendar];
    NSDateComponents* components = [calendar components:flags fromDate:self];
    NSDate* dateOnly = [calendar dateFromComponents:components];
    return dateOnly;
}

- (BOOL) sameDayAsDate:(NSDate*)dateToCompare {
    NSDate *date1 = [self dateWithNoTime];
    NSDate *date2 = [dateToCompare dateWithNoTime];
    return date1 == date2;       // HERE IS WHERE THINGS SEEM TO FAIL

}
你正在比较两个指针值.您需要使用NSDate比较方法,如:

return ([date1 compare:date2] == NSOrderedSame);
http://stackoverflow.com/questions/5629154/compare-two-nsdates-for-same-date-time

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:iphone – 比较两个NSDates相同的日期/时间