Skip to content

Instantly share code, notes, and snippets.

@veritech
Created January 9, 2014 19:48
Show Gist options
  • Save veritech/8340686 to your computer and use it in GitHub Desktop.
Save veritech/8340686 to your computer and use it in GitHub Desktop.
calculate the date at the start and end of a given week
- (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