Objective C, Programming

Objective C – NSString NSDate Conversion

objective-c-nsstring-nsdate-conversion

NSDate convert to NSString:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *strDate = [dateFormatter stringFromDate:[NSDate date]];
NSLog(@"%@", strDate);[dateFormatter release];

Result:

2010-08-04 16:01:03

NSString convert to NSDate:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [dateFormatter dateFromString:@"2010-08-04 16:01:03"];
NSLog(@"%@", date);[dateFormatter release];

Result:

2010-08-04 16:01:03 +0800