Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert from NSString to NSDate?

how to convert from NSString to NSDate ?

it must be in yyyy-MM-dd HH:mm:ss this format when it converts from NSString to NSDate format

like image 576
user217572 Avatar asked Feb 12 '10 12:02

user217572


1 Answers

Use NSDateFormatter's dateFromString:

NSString *dateStr = @"20100212";

// 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:@"YYYY-MM-dd HH:mm:ss"];
dateStr = [dateFormat stringFromDate:date];  
[dateFormat release];
like image 124
peterp Avatar answered Nov 17 '22 14:11

peterp