如何在iphone中比较当前日期与上一个日期?

Hon*_*ney 9 iphone xcode nsdate ios

我想将当前日期与另一个日期进行比较,如果日期早于当前日期,那么我应该停止下一个操作.我怎样才能做到这一点?

我今天有yyyy-MM-dd格式的日期.我需要检查这个条件

if([displaydate text]<currentdate)
{
    //stop next action 
}
Run Code Online (Sandbox Code Playgroud)

如果displaydate小于今天的日期,则必须输入该条件.

Nit*_*hel 36

NSDate *today = [NSDate date]; // it will give you current date
NSDate *newDate = [dateFormatter dateWithString:@"xxxxxx"]; // your date 

NSComparisonResult result; 
//has three possible values: NSOrderedSame,NSOrderedDescending, NSOrderedAscending

result = [today compare:newDate]; // comparing two dates

if(result==NSOrderedAscending)
    NSLog(@"today is less");
else if(result==NSOrderedDescending)
    NSLog(@"newDate is less");
else
    NSLog(@"Both dates are same");
Run Code Online (Sandbox Code Playgroud)

从这个答案得到你的解决方案如何比较Objective-C中的两个日期