xcode 比较两个 NSDate

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8120370/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:25:25  来源:igfitidea点击:

Compare Between 2 NSDate

iosxcodensdate

提问by or azran

i Have 2 NSDateobject the first is set to one month ahead and the seconed is set for today. i want to check if today is after (Bigger) then the month ahead object.

我有 2 个NSDate对象,第一个设置为提前一个月,第二个设置为今天。我想检查今天是否在(更大)之后是前一个月的对象。

How can i do it?

我该怎么做?

回答by winsmith

You can use NSDate's comparemethod. Example:

您可以使用NSDate 的compare方法。例子:

switch ([dateOne compare:dateTwo]) {
case NSOrderedAscending:
    // dateOne is earlier in time than dateTwo
    break;
case NSOrderedSame:
    // The dates are the same
    break;
case NSOrderedDescending:
    // dateOne is later in time than dateTwo
    break;
}

回答by AliSoftware

Read the Date and Time Programming Guidea lot of Date & Time concepts (and how Cocoa handle them) are explained here in details, it is really worth reading.

阅读日期和时间编程指南,这里详细解释了很多日期和时间概念(以及 Cocoa 如何处理它们),非常值得一读。

Also, in the NSDate class Reference, you can find a dedicated section"Comparing dates" for all the methods used to compare two dates. So you should use laterDate:or earlierDate:methods that totally answer your question. You can also use the timeIntervalSinceDate:method and check the sign of the returned time interval (see once again the documentation on this)

此外,在NSDate 类参考中,您可以找到一个专门的部分“比较日期”,用于比较两个日期的所有方法。所以你应该使用laterDate:earlierDate:方法来完全回答你的问题。您还可以使用该timeIntervalSinceDate:方法并检查返回时间间隔的符号(再次参见有关此的文档)

In general, don't hesitate to read the documentationas everythingis already explained in there in detail as you can see given those links

一般来说,请不要犹豫阅读文档,因为所有内容都已在那里详细解释,您可以在这些链接中看到

回答by jbat100

NSDate* earlierDate = [aDate earlierDate:anotherDate];

Return ValueThe earlier of the receiver and anotherDate, determined using timeIntervalSinceDate:. If the receiver and anotherDate represent the same date, returns the receiver.

返回值接收者和 anotherDate 中较早者,使用 timeIntervalSinceDate: 确定。如果接收者和另一个日期表示相同的日期,则返回接收者。

回答by Ron

go to this link:

转到此链接:

http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDate_Class/Reference/Reference.html

http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDate_Class/Reference/Reference.html

other wise convert your date to double and compare it. all related function are in this link.

否则明智地将您的日期转换为双倍并进行比较。所有相关功能都在此链接中。