Created
March 13, 2014 19:48
-
-
Save adamdehaven/9535549 to your computer and use it in GitHub Desktop.
Calculate Dates in Objective-C
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
- (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