Create a gist now

Instantly share code, notes, and snippets.

anonymous /main.m
Created Jan 2, 2013

What would you like to do?
Contrived Date Example
#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