如何比较Objective-C中的日期? [英] How to compare dates in Objective-C?

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

问题描述

我有两个日期:如何在 Objective-C 中比较哪个日期更大?

I have two dates: how do I compare which one is greater date in Objective-C?

推荐答案

来自 NSDate 类参考:

From NSDate class reference:

NSDate -compare: 返回一个 NSComparisonResult表示时间的值接收器和另一个的排序给定日期.

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

  • (NSComparisonResult)compare:(NSDate *)anotherDate

参数 anotherDate 日期与比较接收器.

Parameters anotherDate The date with which to compare the receiver.

这个值不能为零.如果值为零,行为是未定义,将来可能会改变Mac OS X 版本.

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.

返回值如果:

接收者和另一个日期是完全相等,NSOrderedSame

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

接收者在时间上晚于另一个日期,NSOrderedDescending

The receiver is later in time than anotherDate, NSOrderedDescending

接收者在时间上早于另一个日期,NSOrderedAscending.

The receiver is earlier in time than anotherDate, NSOrderedAscending.

讨论 这个方法检测日期之间的亚秒差异.如果您想将日期与粒度较小,使用timeIntervalSinceDate:比较两个日期.

Discussion This method detects sub-second differences between dates. If you want to compare dates with a less fine granularity, use timeIntervalSinceDate: to compare the two dates.

这篇关于如何比较Objective-C中的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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