开发者

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.

  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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜