获取两个 NSDate 之间的所有日期(不是天数)

发布于 2024-12-06 14:50:15 字数 71 浏览 0 评论 0原文

如何获取两个日期之间的所有日期?

例如: 开始日期 = 2011/01/25 结束日期 = 2011/02/03

How can I get all the dates that come in between 2 dates?

For example:
Start date = 2011/01/25
End date = 2011/02/03

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

卖梦商人 2024-12-13 14:50:15

1)找出两个日期之间的天数。然后,

for(i=0;i<noofdays;i++)
{
//Find the next date
//add to the array
}

查找天数

查找下一个日期

1) Find the no of days between two dates. Then,

for(i=0;i<noofdays;i++)
{
//Find the next date
//add to the array
}

To find number of days

To find next date

囚我心虐我身 2024-12-13 14:50:15

NSCalendarUnit 用于定义日期和时间之间的步长。处理日期的标准化。

iOS 8 API、Swift 2.0

    func generateDates(calendarUnit: NSCalendarUnit, startDate: NSDate, endDate: NSDate) -> [NSDate] {

            let calendar = NSCalendar.currentCalendar()
            let normalizedStartDate = calendar.startOfDayForDate(startDate)
            let normalizedEndDate = calendar.startOfDayForDate(endDate)

            var dates = [normalizedStartDate]
            var currentDate = normalizedStartDate

            repeat {

                currentDate = calendar.dateByAddingUnit(calendarUnit, value: 1, toDate: currentDate, options: NSCalendarOptions.MatchNextTime)!
                dates.append(currentDate)

            } while !calendar.isDate(currentDate, inSameDayAsDate: normalizedEndDate)

            return dates
    }

NSCalendarUnit serves for defining the step between the dates & taking care of the dates being normalized.

iOS 8 API, Swift 2.0

    func generateDates(calendarUnit: NSCalendarUnit, startDate: NSDate, endDate: NSDate) -> [NSDate] {

            let calendar = NSCalendar.currentCalendar()
            let normalizedStartDate = calendar.startOfDayForDate(startDate)
            let normalizedEndDate = calendar.startOfDayForDate(endDate)

            var dates = [normalizedStartDate]
            var currentDate = normalizedStartDate

            repeat {

                currentDate = calendar.dateByAddingUnit(calendarUnit, value: 1, toDate: currentDate, options: NSCalendarOptions.MatchNextTime)!
                dates.append(currentDate)

            } while !calendar.isDate(currentDate, inSameDayAsDate: normalizedEndDate)

            return dates
    }
吖咩 2024-12-13 14:50:15

要查找两个 NSDate 之间的所有日期:

- (void)cerateDaysArray{
    _daysArray = [NSMutableArray new];
    NSCalendar *calendar = [[NSCalendaralloc]initWithCalendarIdentifier:NSGregorianCalendar];
    [calendar setTimeZone:[NSTimeZone systemTimeZone]];
    NSDate *startDate = [_minDate copy];
    NSDateComponents *deltaDays = [NSDateComponents new];
    [deltaDays setDay:1];
    [_daysArray addObject:startDate];
    while ([startDate compare:_maxDate] == NSOrderedAscending) {
       startDate = [calendar dateByAddingComponents:deltaDays toDate:startDate options:0];
       [_daysArray addObject:startDate];
   }
}

To find all days between two NSDates:

- (void)cerateDaysArray{
    _daysArray = [NSMutableArray new];
    NSCalendar *calendar = [[NSCalendaralloc]initWithCalendarIdentifier:NSGregorianCalendar];
    [calendar setTimeZone:[NSTimeZone systemTimeZone]];
    NSDate *startDate = [_minDate copy];
    NSDateComponents *deltaDays = [NSDateComponents new];
    [deltaDays setDay:1];
    [_daysArray addObject:startDate];
    while ([startDate compare:_maxDate] == NSOrderedAscending) {
       startDate = [calendar dateByAddingComponents:deltaDays toDate:startDate options:0];
       [_daysArray addObject:startDate];
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文