objective-c - iOS:比较两个日期

我有一个 NSDate 必须与其他两个 NSDate 进行比较,我尝试使用 NSOrderAscendingNSOrderDescending但是如果我的日期与其他两个日期相同?

示例:如果我有一个 myDate = 24/05/2011 和另外两个是一个 = 24/05/2011 和两个 24/05/2011 我可以用什么?

最佳答案

根据Apple documentation of NSDate compare:

Returns an NSComparisonResult value that indicates the temporal ordering of the receiver and another given date.

- (NSComparisonResult)compare:(NSDate *)anotherDate

Parameters anotherDate

The date with which to compare the receiver. This value must not be nil. If the value is nil, the behavior is undefined and may change in future versions of Mac OS X.

Return Value

If:

The receiver and anotherDate are exactly equal to each other, NSOrderedSame

The receiver is later in time than anotherDate, NSOrderedDescending

The receiver is earlier in time than anotherDate, NSOrderedAscending

换句话说:

if ([date1 compare:date2] == NSOrderedSame) ...

请注意,在您的特定情况下,阅读和编​​写此内容可能更容易:

if ([date2 isEqualToDate:date2]) ...

见 Apple Documentation about this one .

https://stackoverflow.com/questions/6112075/

相关文章:

objective-c - Swift 变量是原子的吗?

iphone - presentModalViewController :Animated is d

objective-c - Swift 只读外部,读写内部属性

ios - 如何屏蔽 UIImageView?

objective-c - 如何使用 performSelector :withObject:aft

ios - 如何拦截 MKMapView 或 UIWebView 对象上的触摸事件?

iphone - CALayers 没有在其 UIView 的边界更改时调整大小。为什么?

objective-c - Xcode 4(和依赖项)的编译、构建或存档问题

ios - 带有固定部分标题的 UITableView

ios - 以编程方式检索 iPhone 上的内存使用情况