两个NSDate之间的日期比较

时间:2012-05-24 08:00:14

标签: objective-c ios date nsdate

这是我第一次比较Objective-C中的日期。我一直在网上搜索一段时间,我发现的所有例子都涉及从字符串构建一个NSDate,所以我决定在这里问一个新问题...... 我的问题如下:

我需要知道两个NSD是否在同一天,忽略了时间。我有两个NSArray包含一组日期,我需要逐个确定第一个NSArray中的哪一个与第二个数组中的哪一天。

- (void)setActiveDaysColor:(UIColor *)activeDaysColor
{
    for (DayView *actualDay in _days)
    {
        NSDate *actualDayDate = [actualDay date];

        for (NSDate *activeDayDate in self.dates)
        {
            // Comparison code
            // If both dates are the same, tint actualDay with a different color
        }
    }
}

提前感谢您,祝您度过愉快的一天。

亚历。

5 个答案:

答案 0 :(得分:10)

通过省略时间组件来创建新日期。并使用其中一种比较方法

例如

NSCalendar *calendar = [NSCalendar currentCalendar];
NSInteger components = (NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit);

NSDateComponents *firstComponents = [calendar components:components fromDate:firstDate];
NSDateComponents *secondComponents = [calendar components:components fromDate:secondDate];

NSDate *date1 = [calendar dateFromComponents:firstComponents];
NSDate *date2 = [calendar dateFromComponents:secondComponents];

NSComparisonResult result = [date1 compare:date2];
if (result == NSOrderedAscending) {
} else if (result == NSOrderedDescending) {
}  else {
}

答案 1 :(得分:2)

-(BOOL)isSameDay:(NSDate*)date1 otherDay:(NSDate*)date2 {
NSCalendar* calendar = [NSCalendar currentCalendar];

unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit;
NSDateComponents* comp1 = [calendar components:unitFlags fromDate:date1];
NSDateComponents* comp2 = [calendar components:unitFlags fromDate:date2];

return [comp1 day]   == [comp2 day] &&
[comp1 month] == [comp2 month] &&
[comp1 year]  == [comp2 year];}

答案 2 :(得分:1)

检查NSDate文档,这些是比较日期的方法

  • isEqualToDate
  • earlierDate
  • laterDate
  • 比较

在你的情况下

if([actualDayDate isEqualToDate:activeDayDate])
{

}

答案 3 :(得分:1)

感谢您的所有答案。 我找到了一个更清晰的回答我的问题在一个完全不相关的帖子中回答,但实际上效果很好。

if ((unsigned int)[actualDayDate timeIntervalSinceDate:activeDayDate] / 60 / 60 / 24 == 0)
{
     // do Stuff
}

答案 4 :(得分:1)

您可以使用谓词来过滤掉今天的所有对象,而不是代码中的循环。过滤掉今天的日期是通过将其与今天的开始和今天的结束进行比较来完成的。

您可以将任意NSDate设置为该日期的开头(参见this answer

NSDate *beginDate = [NSDate date];
[[NSCalendar currentCalendar] rangeOfUnit:NSDayCalendarUnit startDate:&beginDate interval:NULL forDate:beginDate];

然后要获得结束日期,您只需添加一天即可。 不要通过计算秒数来添加天数。这不适用于夏令时!这样做(另见this answer):

NSDateComponents *oneDay = [[NSDateComponents alloc] init];
[oneDay setDay:1];
// one day after begin date
NSDate *endDate = [[NSCalendar currentCalendar] dateByAddingComponents:oneDay toDate:beginDate options:0];

现在您有两个日期定义了今天的范围,您可以使用NSPredicate过滤所有DayView,以获取当天所有DayView的新数组,如下所示(请参阅this answer):

// filter DayViews to only include those where the day is today
NSArray *daysThatAreToday = [_days filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(date >= %@) AND (date <= %@)", beginDate, endDate]];

现在,您可以通过枚举新数组(包含今天的DayViews)将色调颜色应用于所有DayViews

[daysThatAreToday enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    // Set the tint color here...
}];

在我看来,这是一个干净的,但更重要的是正确 解决问题的方法。它清晰地读取并处理夏令时和其他日历,然后格里高利。如果您想在特定的一周(或任何其他时间段)为所有DayView着色,也可以轻松地重复使用。