比较两个相同日期/时间的NSDates [英] Compare two NSDates for same date/time
本文介绍了比较两个相同日期/时间的NSDates的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
date1 == date2
不是一种有效的比较方法吗?如果没有,那么正确的选择是什么?
Is date1 == date2
not a valid way to compare? If not, what is the correct alternative?
这是我的代码:
- (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比较方法,如:
You're comparing two pointer values. You need to use the NSDate comparison method like:
return ([date1 compare:date2] == NSOrderedSame);
这篇关于比较两个相同日期/时间的NSDates的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文