Contrived Date Example
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
#import <Foundation/Foundation.h> | |
int main(int argc, const char * argv[]) | |
{ | |
@autoreleasepool { | |
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; | |
NSDateFormatter *df = [[NSDateFormatter alloc] init]; | |
[df setDateFormat:@"YYYY/MM/dd hh:mm:ss"]; | |
NSDateComponents *components = [[NSDateComponents alloc] init]; | |
[components setYear:2012]; | |
[components setMonth:12]; | |
[components setDay:31]; | |
NSDate *nye = [gregorian dateFromComponents:components]; | |
NSString *dateStr = [df stringFromDate:nye]; | |
NSLog(@"NYE: %@", dateStr); | |
NSDate *now = [NSDate date]; | |
[df setDateFormat:@"yyyy/MM/dd hh:mm:ss"]; | |
NSDate *reconstitutedDate = [df dateFromString:dateStr]; | |
NSLog(@"NYE: %@", reconstitutedDate); | |
} | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment