Created
January 9, 2014 19:48
-
-
Save veritech/8340686 to your computer and use it in GitHub Desktop.
calculate the date at the start and end of a given week
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
- (NSDate *)dateAtStartOfWeek | |
{ | |
NSCalendar *calendar = [NSDate AZ_currentCalendar]; | |
NSDateComponents *components = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSWeekdayCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:self]; | |
// NSRange range = [calendar rangeOfUnit:NSDayCalendarUnit inUnit:NSWeekdayCalendarUnit forDate:self]; | |
components.day -= components.weekday; | |
return [calendar dateFromComponents:components]; | |
} | |
- (NSDate *)dateAtEndOfWeek | |
{ | |
NSCalendar *calendar = [NSDate AZ_currentCalendar]; | |
NSDateComponents *components = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSWeekdayCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:self]; | |
NSRange range = [calendar rangeOfUnit:NSDayCalendarUnit inUnit:NSWeekCalendarUnit forDate:self]; | |
components.day += range.length - components.weekday; | |
return [calendar dateFromComponents:components]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment