Created
April 8, 2013 18:01
-
-
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.
This file contains hidden or 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
// 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