获取2个日期之间的日期的NSDateComponents具有不正确的日期属性

时间:2014-10-28 17:29:29

标签: ios objective-c nsdate nscalendar nsdatecomponents

我正在使用NSDateComponents和NSCalendar获取两个日期之间的日期。

// Get dates between before and after
NSDateComponents *betweenDateComponents = [calendar components:componentFlags
                                                        fromDate:afterDate
                                                          toDate:beforeDate
                                                         options:0];

然后我有一个简单的for循环,它基于组件的day属性:

  for (int i = 1; i < betweenDateComponents.day; ++i) {

当我在一周或一个月的所有日子里工作时,这非常有效。如果我按周过滤,则day属性记录为6.如果我按月过滤day属性为29,但出于某种原因,当我按年过滤时,day属性记录为29而不是364。

为什么当我尝试工作一年时,白天的财产总是错误的?

这是我的所有代码,直到上面包含的for循环:

// Set the date components according to the type of filter we're doing
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [NSDateComponents new];
    if (filterType == CommentFilterTypeWeeklyByDay) {
        [components setDay:-6];
    } else if (filterType == CommentFilterTypeMonthlyByDay) {
        [components setDay:-29];
    } else if (filterType == CommentFilterTypeYearlyByMonth) {
        [components setDay:-364];
    }

    NSUInteger componentFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;

    // Zero out before and after dates
    NSDate *beforeDate = [NSDate date];
    NSDateComponents *zeroComponents = [[NSCalendar currentCalendar] components:componentFlags fromDate:beforeDate];
    beforeDate = [calendar dateFromComponents:zeroComponents];

    NSDate *afterDate = [calendar dateByAddingComponents:components toDate:beforeDate options:0];
    zeroComponents = [[NSCalendar currentCalendar] components:componentFlags fromDate:afterDate];
    afterDate = [calendar dateFromComponents:zeroComponents];

    NSMutableArray *dates = [NSMutableArray new];
    [dates addObject:afterDate];

    // Get dates between before and after
    NSDateComponents *betweenDateComponents = [calendar components:componentFlags
                                                          fromDate:afterDate
                                                            toDate:beforeDate
                                                           options:0];
    NSLog(@"%d", betweenDateComponents.day);

    for (int i = 1; i < betweenDateComponents.day; ++i) {

1 个答案:

答案 0 :(得分:1)

由于您的组件包含NSYearCalendarUnitNSMonthCalendarUnit,我怀疑您必须使用monthyear而不是day。这些成分是累积的,而不是独立的。

通过更改components

,您将始终只能获得天数(无论您的日期之间有多少个月或几年)
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [NSDateComponents new];

switch (filterType) {
  case CommentFilterTypeWeeklyByDay: [components setDay:-6]; break;
  case CommentFilterTypeMonthlyByDay: [components setDay:-29]; break;
  case CommentFilterTypeYearlyByMonth: [components setDay:-364]; break;
  case CommentFilterTypeCustom: [components setDay:-590]; break;
  default: break;
}

NSUInteger componentFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
NSDate *beforeDate = [NSDate date];
NSDateComponents *zeroComponents = [[NSCalendar currentCalendar] components:componentFlags fromDate:beforeDate];
beforeDate = [calendar dateFromComponents:zeroComponents];

NSDate *afterDate = [calendar dateByAddingComponents:components toDate:beforeDate options:0];
zeroComponents = [[NSCalendar currentCalendar] components:componentFlags fromDate:afterDate];
afterDate = [calendar dateFromComponents:zeroComponents];

NSDateComponents *betweenDateComponents = [calendar components:NSDayCalendarUnit
                                                      fromDate:afterDate
                                                        toDate:beforeDate
                                                       options:0];

NSLog(@"%d", betweenDateComponents.day);

此处NSLog打印

6

CommentFilterTypeWeeklyByDay 29

CommentFilterTypeMonthlyByDay 364

CommentFilterTypeYearlyByMonth 590

CommentFilterTypeCustom