views:

27

answers:

2

I have a string in the form of

"11:00AM, Saturday August 21, 2010"

How do I convert this to an NSDate object?

+1  A: 

Use a NSDateFormatter for this format. -(NSDate *)dateFromString:(NSString *)string is the method you want to use then.

Eiko
any idea of how to configure that date formatter for such a request?
Aran Mulholland
+1  A: 

This should work:

NSString *input = @"11:00AM, Saturday August 21, 2010";
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
[df setDateFormat:@"hh:mma, EEEE MMMM d, yyyy"];
NSDateFormatter *df2 = [[[NSDateFormatter alloc] init] autorelease];
[df2 setDateFormat:@"MMMM yyyy"];
NSDate *date = [df2 stringFromDate:[df dateFromString:input]];


And format your date object as you want.
http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns

Simon
if this was the right solution for you, please mark it as correct answer.
Simon
The first DateFormatter you set up was fine, (df). I don't know what you were doing with the second date formatter, but the first one worked fine. Thanks.
Aran Mulholland