Skip to content

Instantly share code, notes, and snippets.

@adamdehaven
Created March 13, 2014 19:48
Show Gist options
  • Save adamdehaven/9535549 to your computer and use it in GitHub Desktop.
Save adamdehaven/9535549 to your computer and use it in GitHub Desktop.
Calculate Dates in Objective-C
- (void) calculateDates {
NSDate *todayDate = [[NSDate alloc] init];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *tomorrowOffset = [[NSDateComponents alloc] init];
[tomorrowOffset setDay:1];
NSDateComponents *yesterdayOffset = [[NSDateComponents alloc] init];
[yesterdayOffset setDay:-1];
NSDate *tomorrowDate = [gregorian dateByAddingComponents:tomorrowOffset toDate:todayDate options:0];
NSDate *yesterdayDate = [gregorian dateByAddingComponents:yesterdayOffset toDate:todayDate options:0];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MM-dd-yyyy"];
NSString *yesterday = [dateFormatter stringFromDate: yesterdayDate];
NSString *today = [dateFormatter stringFromDate: todayDate];
NSString *tomorrow = [dateFormatter stringFromDate: tomorrowDate];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment