I have an exact string as say "November 4, 2010" and I want to compare it with today's date in iPhone?
How can one convert开发者_如何转开发 a string "November 4, 2010" and not "MMMM d, YYYY" to NSDate
for comparison?
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.
精彩评论