带有NSDate条目的NSMutableArray获取天数

时间:2013-06-21 17:08:53

标签: ios objective-c nsmutablearray nsdate

我有NSDates的NSMutableArray,有些日期是在同一天。获取特定日期的NSDates数量的最佳方法是什么?

编辑:

对不起,我没有完全解释。我需要某种逻辑来从源数组中获取特定日期的日期数。所以我遍历数组,找到确切的日期,我需要知道同一天数组中有多少天。

2 个答案:

答案 0 :(得分:1)

修改

NSDate *sourceDate = theDateThatYouAreLookingFor;
NSCalendar *calendar = [NSCalendar currentCalendar];
[calendar setTimeZone:[NSTimeZone systemTimeZone]];

NSDateComponents *dateComponents = [calendar components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit) fromDate:sourceDate];    

NSInteger day = [dateComponents day];
NSInteger month = [dateComponents month];
NSInteger year = [dateComponents year];

int count = 0;
//add check for existing dates on the same day
for(NSDate *checkDate in datesArray)
{
    dateComponents = [calendar components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit) fromDate:checkDate];    

    NSInteger checkDay = [dateComponents day];
    NSInteger checkMonth = [dateComponents month];
    NSInteger checkYear = [dateComponents year];

    if(checkDay == day && checkMonth == month && checkYear == year)
        count++;

}
//do something with count

结束编辑

最简单的方法是遍历数组并使用NSDateFormatter将每个日期的日期作为字符串进行比较。请记住,NSDateFormatters很昂贵,所以你不想经常执行这个过程。

您可以这样做:

NSMutableDictionary *dateInfo = [NSMutableDictionary dictionary];


NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
formatter.locale = [NSLocale currentLocale];
formatter.dateFormat = @"yyyyMMdd";

for(NSDate *date in datesArray)
{
    NSString *dateStr = [formatter stringFromDate:date];
    NSNumber *currentCount = [dateInfo objectForKey:dateStr];
    if(currentCount)
    {
        //bump value
        [dateInfo setObject:[NSNumber numberWithInt:currentCount.intValue + 1] forKey:dateStr];
    }
    else
    {
        [dateInfo setObject:[NSNumber numberWithInt:1] forKey:dateStr];
    }
}

答案 1 :(得分:1)

出于性能原因,我会创建两个日期,“今天”(00:00:00)和“明天”(00:00:00)。您可以将这些日期用作比较的边界。如果日期等于或大于今天且小于明天则是匹配。

由于NSDate比较基本上是双(原始类型)比较,因此此方法比比较NSString对象或NSDateComponents更快。要创建NSDateComponents,需要使用大量数学,NSDateFormatters本身可能使用NSDateComponents。

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSDateComponents *todayComponents = [[NSDateComponents alloc] init];
todayComponents.year = 2013;
todayComponents.month = 6;
todayComponents.day = 21;

// today must be at the beginning of your search day. i.e. 00:00:00
NSDate *today = [calendar dateFromComponents:todayComponents]; 

NSDateComponents *oneDayOffset = [[NSDateComponents alloc] init];
oneDayOffset.day = 1;
NSDate *tomorrow = [calendar dateByAddingComponents:oneDayOffset toDate:today options:0];

NSIndexSet *todaysIndexes = [array indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
    if ([(NSDate *)obj compare:today] != NSOrderedAscending && [(NSDate *)obj compare:tomorrow] == NSOrderedAscending) {
//       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ obj is not earlier than "today" (= same time or later)
//                                                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ obj is earlier than "tomorrow"            
        return YES;
    }
    return NO;
}];

NSLog(@"%d", [todaysIndexes count]);

今天替换您的搜索日期(午夜),您就可以开始了。