0

How can one convert a string "November 4, 2010" and not "MMMM d, YYYY" to NSDate for comparison?

1 Answer 1

3

You can use a NSDateFormatter, for this behaviour.

eg.

NSString *string = @"November 4, 2010";
[NSDateFormatter setDefaultFormatterBehavior:NSDateFormatterBehavior10_0];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"%B %e, %j";
NSDate *wantedDate = [formatter dateFromString:string];

[[Date date] compareWithDate:wantedDate]; // will return basically, what you want

See the NSDateFormatter class reference, and the Data Formatting Guide. For background information about date formatting see: here.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.