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

前端之家收集整理的这篇文章主要介绍了ios – 如何比较当前日期与iPhone之前的日期?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想将当前日期与另一个日期进行比较,如果该日期早于当前日期,那么我应该停止下一个操作.我该怎么做?

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

  1. if([displaydate text]<currentdate)
  2. {
  3. //stop next action
  4. }

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

解决方法

  1. NSDate *today = [NSDate date]; // it will give you current date
  2. NSDate *newDate = [dateFormatter dateWithString:@"xxxxxx"]; // your date
  3.  
  4. NSComparisonResult result;
  5. //has three possible values: NSOrderedSame,NSOrderedDescending,NSOrderedAscending
  6.  
  7. result = [today compare:newDate]; // comparing two dates
  8.  
  9. if(result==NSOrderedAscending)
  10. NSLog(@"today is less");
  11. else if(result==NSOrderedDescending)
  12. NSLog(@"newDate is less");
  13. else
  14. NSLog(@"Both dates are same");

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

猜你在找的iOS相关文章