仅按年月日比较 NSDate 对象
如果我想使用compare方法来比较两天:
[day1 compare:day2] == NSOrderedAscending
如何仅按年月日进行比较?
If I want to use the compare method to compare two days:
[day1 compare:day2] == NSOrderedAscending
How to compare only by year-month-day?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一天有 86400 秒,因此如果您获取两个日期自参考日期以来的秒数,则可以将它们除以 86400 并比较这两个数字。
这将为您提供(以
days1
和days2
为单位)自 1970 年 1 月 1 日起过去的日期的天数。您可以然后比较它们。There are 86400 seconds in a day, so if you get the number of seconds since a reference date for both dates, you can divide them by 86400 and compare the two numbers.
This will give you (in
days1
anddays2
) the number of days for the dates that have passed since January 1st, 1970. You can then compare them.理想情况下,您希望编写如下内容:
您可以通过 NSDate 上的简单类别来实现此目的,例如:
Ideally, you would like to write something like:
You can achieve this with a simple category on NSDate, sonething like: