Skip to content

Instantly share code, notes, and snippets.

Created January 2, 2013 22:57
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save anonymous/4439095 to your computer and use it in GitHub Desktop.
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