I'm trying to get the dates within a given month.
My plan is to
- Get the start and the end dates of a given month,.
- Get all the dates that fall within that range.
- Iterate through them and eliminate dates that fall within a weekend using the
isDateInWeekend
method.
The remaining dates are the weekdays.
So I created two NSDate
extension methods to get the start and the end dates of the month.
extension NSDate {
var startOfMonth: NSDate {
let calendar = NSCalendar.currentCalendar()
let components = calendar.components([.Year, .Month], fromDate: self)
return calendar.dateFromComponents(components)!
}
var endOfMonth: NSDate {
let calendar = NSCalendar.currentCalendar()
let components = NSDateComponents()
components.month = 1
return (calendar.dateByAddingComponents(components, toDate: self.startOfMonth, options: NSCalendarOptions())?.dateByAddingTimeInterval(-1))!
}
}
Now I'm stuck at step #2. I can't find a way to get a range of dates given a start and an end date.
Is there a way to do this?
With the help of this answer, I was able to accomplish this.
And it works!