如何在iphone中比较当前日期与上一个日期? [英] How to compare current date to previous date in iphone?

查看:121
本文介绍了如何在iphone中比较当前日期与上一个日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to compare the current date with another date, and if that is date is earlier than the current date, then I should stop the next action. How can I do this?

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

I have todays date in yyyy-MM-dd format. I need to check this condition

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

此处如果 displaydate 小于今天的日期,然后必须输入该条件。

Here if displaydate is less than todays date then it has to enter that condition.

推荐答案

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

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

got your solution from this answer How to compare two dates in Objective-C

这篇关于如何在iphone中比较当前日期与上一个日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆