Friday, May 6, 2011

Creating NSDate object from NSString using NSDateFormatter example

This example shows how to create an NSDate object from NSString using NSDateFormatter. The conversion work is done by NSDateFormatter object. See following code, it's simple enough to understand.


NSString *dateString = @"2010-04-16";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

// 포맷에 주의!
[dateFormatter setDateFormat:@"yyyy-MM-dd"];

NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];