public
anonymous / main.m
Created

Contrived Date Example

  • Download Gist
main.m
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
#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;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.