获得几天之间的日子

时间:2014-11-11 11:10:41

标签: ios objective-c nsdate nsdateformatter nsdatecomponents

在给予downvote之前,请评论原因

我创建了一个UIDatePicker,其中包含最小值和最小值。最大日期值(即日期选择器的6个月)现在,我需要从所选日期开始7天。我需要检查条件,

  • 如果日期是今天的日期,我需要从今天开始7天
  • 如果日期是最后日期(即选民的最后日期)需要过去7天,包括最后一天
  • 如果日期是今天的中间日期&我需要最后3天,接下来3天,包括今天的日期。并且,同时获得最后&未来3天,它不应超过选择者的日期限制。

Here's my code snippet

- (void)addDays:(NSInteger)range {
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"dd-MM-yyyy"];

    NSDate *startDate = self.selectedDate;

    for (int x = 0; x <= range; x++) {
        NSLog(@"%@", [dateFormat stringFromDate:startDate]);
        startDate = [startDate dateByAddingTimeInterval:(60 * 60 * 24)];
    }
}

- (void)minusDays:(NSInteger)range {
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"dd-MM-yyyy"];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *comps = [NSDateComponents new];

    for (NSInteger i=0; i<range; i++) {
        comps.day += -1;

        NSDate *date = [calendar dateByAddingComponents:comps toDate:self.selectedDate options:0];
        NSDateComponents *components = [calendar components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit) fromDate:date];
        NSLog(@"%@", [dateFormat stringFromDate:[calendar dateFromComponents:components]]);
    }
}

- (void)calculateDateRange {

    if ([dateArray count] > 0) {
        [dateArray removeAllObjects];
    }

    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *lastcomponents = [calendar components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit) fromDate:[NSDate date]];
    lastcomponents.month += 6;

    NSDateFormatter *formatter = [NSDateFormatter new];
    [formatter setDateFormat:@"yyyy/MM/dd"];

    NSDate *currentDate = [NSDate date];
    NSDate *selectedD = self.selectedDate;
    NSDate *endDate = [calendar dateFromComponents:lastcomponents];

    NSDate *fromDate;
    NSDate *toDate;

    [calendar rangeOfUnit:NSDayCalendarUnit startDate:&fromDate interval:NULL forDate:currentDate];
    [calendar rangeOfUnit:NSDayCalendarUnit startDate:&toDate interval:NULL forDate:selectedD];
    NSDateComponents *difference = [calendar components:NSDayCalendarUnit fromDate:fromDate toDate:toDate options:0];
    NSInteger first = [difference day];

    [calendar rangeOfUnit:NSDayCalendarUnit startDate:&fromDate interval:NULL forDate:selectedD];
    [calendar rangeOfUnit:NSDayCalendarUnit startDate:&toDate interval:NULL forDate:endDate];
    NSDateComponents *difference2 = [calendar components:NSDayCalendarUnit fromDate:fromDate toDate:toDate options:0];
    NSInteger second = [difference2 day];

    if ((first == 0 || first < 3) && second > 7) {
        [self addDays:7];
    } else if (first >= 3 && second > 7) {
        [self minusDays:3];
        [self addDays:3];
    }else if (second == 7 || second < 7) {
        [self minusDays:7];
    }
}

这很好用。但是,不能得到确切的最后&amp;前几天。

有人对此有所了解吗?

1 个答案:

答案 0 :(得分:2)

我对您的需求的解释

你有一个日期选择器。选择日期后,您需要在所选日期周围创建7天range

因此,如果所选日期是2014年11月15日,则您需要3天左右,所以......

12/11/2014 - 18/11/2014.

但是,日期范围不能超过日期选择器的限制。因此,如果日期选择器上的最小日期设置为14/11/2014,那么(在上面的示例中)日期范围将是......

14/11/2014 - 21/11/2014

即使在你的额外解释之后,这仍然是我的解释。而我的代码就是这样做的。

<强>解决方案

不能使用60 * 60 * 24表示一天。这是错的。处理日期时,您应始终使用NSDateComponentsNSCalendar

此外,将问题分解为小步骤。没有理由在一个巨大的功能中做所有事情。

好吧我猜你在某个地方有一个datePicker动作所以我会像这样编码......

- (void)datePickerDateChanged
{
    NSDate *minimumDate = self.datePicker.minimumDate;
    NSDate *maximumDate = self.datePicker.maximumDate;

    NSDate *selectedDate = self.datePicker.date;

    NSDate *startDate;
    NSDate *endDate;

    if ([self numberOfDaysFromDate:minimumDate toDate:selectedDate] < 3) {
        // get 7 days after minimumDate
        startDate = minimumDate;
        endDate = [self dateByAddingDays:6 toDate:minimumDate];
    } else if ([self numberOfDaysFromDate:selectedDate toDate:maximumDate] < 3) {
        // get 7 days before maximumDate
        startDate = [self dateByAddingDays:-6 toDate:maximumDate];
        endDate = maximumDate;
    } else {
        // get 3 days before and 3 days after selectedDate
        startDate = [self dateByAddingDays:-3 toDate:selectedDate];
        endDate = [self dateByAddingDays:3 toDate:selectedDate];
    }
    // Here startDate and endDate define your date range.

}

- (NSDate *)dateByAddingDays:(NSInteger)days toDate:(NSDate *)date
{
    NSCalendar *calendar = [NSCalendar currentCalendar];

    NSDateComponents *components = [NSDateComponents new];
    components.day = days;

    return [calendar dateByAddingComponents:components toDate:date options:0];
}

- (NSInteger)numberOfDaysFromDate:(NSDate *)fromDate toDate:(NSDate *)toDate
{
    NSCalendar *calendar = [NSCalendar currentCalendar];

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

    // always return positive. We just want magnitude of days.
    return components.day > 0 ? components.day : -components.day;
}

这是未经测试的,只是第一次尝试。