Created
March 11, 2015 22:19
-
-
Save netmask/9857b66f720bce28572c to your computer and use it in GitHub Desktop.
Simple Swift NSDate Iterator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
infix operator .. {} | |
extension NSDate { | |
var components: NSDateComponents{ | |
var calendar = NSCalendar(calendarIdentifier: NSGregorianCalendar) | |
var flags: NSCalendarUnit = .DayCalendarUnit | .WeekdayCalendarUnit | .YearCalendarUnit | .MonthCalendarUnit | .HourCalendarUnit | |
return calendar!.components(flags, fromDate: self) | |
} | |
} | |
func .. (let left:NSDate, right:NSDate) -> [NSDate]{ | |
let calendar = NSCalendar.currentCalendar() | |
var dates:[NSDate] = [left] | |
while(right.compare(dates.last!) != NSComparisonResult.OrderedSame){ | |
dates.append(calendar.dateByAddingUnit(NSCalendarUnit.DayCalendarUnit, | |
value: 1, toDate: dates.last!, options: nil)!) | |
} | |
return dates | |
} | |
let range = NSDate..NSDate | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment