A problem when converting string to date in iPhone?
How t开发者_如何学Goo convert NSString 2010-08-03 04:37:31.0 to August 3, 2010?
Is it possible?
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.
- The input string is that it lacks timezone data
- Different cultures expect a different order than Month Day Year. That can be fixed if you use one of the generic
NSDateFormatterStyle
formats likeNSDateFormatterLongStyle
.
Check out the NSDateFormatter documentation. You probably want a format string of @"%B %e, %Y"
for the output.
精彩评论