开发者

I have an exact string as say "November 4, 2010" and I want to compare it with today's date in iPhone?

开发者 https://www.devze.com 2023-01-16 20:35 出处:网络
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.

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.

0

精彩评论

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