RAGOpoR
RAGOpoR

Reputation: 8168

A problem when converting string to date in iPhone?

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

Is it possible?

Upvotes: 0

Views: 84

Answers (2)

wm_eddie
wm_eddie

Reputation: 3958

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.

Upvotes: 3

Carl Norum
Carl Norum

Reputation: 225262

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

Upvotes: 0

Related Questions