Converting a string to an NSDate

hugo411 picture hugo411 · Feb 22, 2010 · Viewed 75.5k times · Source

How is it possible to convert a string to an NSDate on iOS?

Answer

Suresh Thayu picture Suresh Thayu · Feb 23, 2010
NSString *dateStr = @"20100223";

// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyyMMdd"];
NSDate *date = [dateFormat dateFromString:dateStr];  

// Convert date object to desired output format
[dateFormat setDateFormat:@"EEEE MMMM d, YYYY"];
dateStr = [dateFormat stringFromDate:date];  
[dateFormat release];

Hope this will help you.