之间比较2 NSDate的

问题描述:

我有第一个被设置为提前一个月2 NSDate对象和seconed设置为今天。 我想检查今天是否在(较大)之后的月份前的对象。之间比较2 NSDate的

我该怎么办呢?

阅读Date and Time Programming Guide很多的日期&时间概念(以及如何处理可可他们)进行了详细的解释在这里,真的很值得一读。

另外,在用于比较两个日期的所有方法的NSDate class Reference, you can find a dedicated section“比较日期”。所以,你应该用laterDate:earlierDate:方法是完全回答你的问题。您也可以使用timeIntervalSinceDate:方法和检查返回的时间间隔的符号(再一次看到这个文档)

一般情况下,不要犹豫,阅读文档一切已经解释在那里详细,你可以看到给出的链接

去这个链接:

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

其他明智的转换你的约会增加一倍,并进行比较。所有相关的功能都在这个环节。

您可以使用NSDate's compare method。例如:

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; 
} 

NSDate* earlierDate = [aDate earlierDate:anotherDate]; 

返回值 接收器和anotherDate的早期,使用timeIntervalSinceDate确定:。如果接收方和anotherDate表示相同的日期,则返回接收方。