views:

48

answers:

2

How to convert NSString 2010-08-03 04:37:31.0 to August 3, 2010?

Is it possible?

A: 

Check out the NSDateFormatter documentation. You probably want a format string of @"%B %e, %Y" for the output.

Carl Norum
thank for your advise, Carl Norum
RAGOpoR
+3  A: 

This is probably a duplicate but NSDateFormatter is what you are looking for.

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.S"];
NSDate *date = [dateFormatter dateFromString:inDateString];

[dateFormatter setDateFormat:@"MMMM dd',' yyyy"];
NSString *outDateString = [dateFormatter stringFromDate:date];

There are two problems with this approach though.

  1. The input string is that it lacks timezone data
  2. Different cultures expect a different order than Month Day Year. That can be fixed if you use one of the generic NSDateFormatterStyle formats like NSDateFormatterLongStyle.
wm_eddie
thank wm_eddie, this code it work great!
RAGOpoR