Skip to content

Instantly share code, notes, and snippets.

@joshbuhler
Created April 8, 2013 18:01
Show Gist options
  • Save joshbuhler/5338984 to your computer and use it in GitHub Desktop.
Save joshbuhler/5338984 to your computer and use it in GitHub Desktop.
Simple method for calculating a date that was one week, one month, 6 months, or a year ago.
// DateFilter is just an enum for some preset dates to filter on.
- (NSDate *) dateForFilter:(DateFilter)filterType
{
NSDate *today = [NSDate date];
NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComp = [[NSDateComponents alloc] init];
switch (filterType) {
case DateFilterWeek:
dateComp.week = -1;
break;
case DateFilterMonth:
dateComp.month = -1;
break;
case DateFilter6Month:
dateComp.month = -6;
break;
case DateFilterYear:
dateComp.year = -1;
break;
default:
break;
}
NSDate *pastDate = [cal dateByAddingComponents:dateComp toDate:today options:0];
return pastDate;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment