-
-
Notifications
You must be signed in to change notification settings - Fork 771
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
Comments
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) |
I guess a pre-defined time to apply should be optional, but with default value |
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) |
Thank you, I've just added it to 4.0.8 release both for Below an example with 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 let from = DateInRegion().startOf(component: .day)
let to = from + 30.days
let dates = DateInRegion.dates(between: from, and: to, increment: 1.day) |
Awesome!👍👏 |
I have idea to impelement method, that will give you Array of Date objects between two dates
The text was updated successfully, but these errors were encountered: