Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert date from one format to another format

Tags:

objective-c

HI all,

i have a date string like this

2011-03-31 13:32:02

i want to convert this date string to display like this

Thursday – March 31, 2011

Please help to solve this

Thanks in advance

like image 586
Naveen Avatar asked Dec 07 '22 22:12

Naveen


1 Answers

Use NSDateFormatter. First initialize the formatter with the appropriate format to parse the source string, something like this:

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

Then use it to parse the date string:

NSDate *date = [formatter dateFromString:string];

Now you can create a new formatter to output in the new format, or just reuse the existing one:

[formatter setDateFormat:@"EEEE - MMMM dd, yyyy"];
NSString *output = [formatter stringFromDate:date];
like image 170
Anomie Avatar answered Dec 30 '22 22:12

Anomie