Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enumerate dates between two intervals (Date/DateInRegion.dates(between:and:increment) #310

Closed
artemkalinovsky opened this issue Oct 20, 2016 · 5 comments
Assignees
Milestone

Comments

@artemkalinovsky
Copy link

artemkalinovsky commented Oct 20, 2016

I have idea to impelement method, that will give you Array of Date objects between two dates

static func datesBetween(startDate:Date, endDate: Date) -> [Date] 
@malcommac
Copy link
Owner

a sort of enumeration of dates; we could also give the opportunity to set a pre-defined time to apply to each enumerated date (optional maybe)

@artemkalinovsky
Copy link
Author

artemkalinovsky commented Oct 20, 2016

I guess a pre-defined time to apply should be optional, but with default value

@artemkalinovsky
Copy link
Author

artemkalinovsky commented Oct 20, 2016

At the moment I've implemented it in this way:

extension Date {
    static func datesBetween(startDate:Date, endDate: Date, addingDateComponents: DateComponents) -> [Date] {
        let calendar = NSCalendar.current

        let normalizedStartDate = calendar.startOfDay(for: startDate)
        let normalizedEndDate = calendar.startOfDay(for: endDate)

        var dates = [normalizedStartDate]
        var currentDate = normalizedStartDate

        repeat {
            currentDate = calendar.date(byAdding: addingDateComponents, to: currentDate)!
            dates.append(currentDate)

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

        return dates
    }
}

And using it:

  months = Date.datesBetween(startDate: startDate,
                                       endDate: endDate,
                                       addingDateComponents: 1.months)

@malcommac malcommac added this to the 4.0.8 milestone Nov 30, 2016
@malcommac malcommac self-assigned this Nov 30, 2016
malcommac added a commit that referenced this issue Nov 30, 2016
- #310: Added dates(between:and:increment:) both to DateInRegion and Date; it allows you to enumerate dates between two intervals.
- #348:  Added && operator to merge DateComponents instances
@malcommac
Copy link
Owner

malcommac commented Nov 30, 2016

Thank you, I've just added it to 4.0.8 release both for DateInRegion and Date instances.
feddce9

Below an example with Date:

let from = Date().startOf(component: .day)
let to = from + 30.days
let dates = Date.dates(between: from, and: to, increment: 1.day)

It's pretty similar with DateInRegion:

let from = DateInRegion().startOf(component: .day)
let to = from + 30.days
let dates = DateInRegion.dates(between: from, and: to, increment: 1.day)

@malcommac malcommac changed the title Implement method to create [Date] from start date and end date Enumerate dates between two intervals (Date/DateInRegion.dates(between:and:increment) Nov 30, 2016
@artemkalinovsky
Copy link
Author

Awesome!👍👏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants