iOS:比较两个日期 [英] iOS: Compare two dates

查看:146
本文介绍了iOS:比较两个日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a NSDate that I must compare with other two NSDate and I try with NSOrderAscending and NSOrderDescending but if my date is equal at other two dates?

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

Example: if I have a myDate = 24/05/2011 and other two that are one = 24/05/2011 and two 24/05/2011 what can I use?

推荐答案

根据 Apple文档 NSDate比较:


返回一个NSComparisonResult值,指示接收者的时间顺序和另一个给定日期。

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

- (NSComparisonResult)compare:(NSDate *)anotherDate

参数 anotherD吃了

比较
接收者的日期。该值不得为零。
如果值为nil,则行为为
undefined,并且可能在将来的
版本的Mac OS X中发生变化。

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.

返回值

如果:

接收者和anotherDate完全相等
相互之间,
NSOrderedSame

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

接收者稍后是
时间比anotherDate,
NSOrderedDescending

The receiver is later in time than anotherDate, NSOrderedDescending

收件人是
早于其他日期,
NSOrderedAscending

The receiver is earlier in time than anotherDate, NSOrderedAscending

换句话说:

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

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

Note that it might be easier in your particular case to read and write this :

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

参见关于这个的Apple文档

这篇关于iOS:比较两个日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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