ios – 如何比较当前日期与iPhone之前的日期?

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

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

if([displaydate text]<currentdate)
{
    //stop next action 
}

这里如果displaydate小于今天的日期,那么它必须输入该条件.

解决方法

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");

从这个答案How to compare two dates in Objective-C得到你的解决方

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...