比较两个 NSDate 并忽略时间部分
比较两个 NSDate 的最有效/推荐的方法是什么?我希望能够查看两个日期是否在同一天,无论时间如何,并开始编写一些代码,这些代码使用 NSDate 类中的 timeIntervalSinceDate: 方法并获取该值的整数除以秒数一天之内。这看起来很冗长,我觉得我错过了一些明显的东西。
我试图修复的代码是:
if (!([key compare:todaysDate] == NSOrderedDescending))
{
todaysDateSection = [eventSectionsArray count] - 1;
}
其中 key 和 TodaysDate 是 NSDate 对象,并且 TodaysDate 正在使用以下命令创建:
NSDate *todaysDate = [[NSDate alloc] init];
问候
戴夫
What is the most efficient/recommended way of comparing two NSDates? I would like to be able to see if both dates are on the same day, irrespective of the time and have started writing some code that uses the timeIntervalSinceDate: method within the NSDate class and gets the integer of this value divided by the number of seconds in a day. This seems long winded and I feel like I am missing something obvious.
The code I am trying to fix is:
if (!([key compare:todaysDate] == NSOrderedDescending))
{
todaysDateSection = [eventSectionsArray count] - 1;
}
where key and todaysDate are NSDate objects and todaysDate is creating using:
NSDate *todaysDate = [[NSDate alloc] init];
Regards
Dave
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
在进行比较之前,您将日期中的时间设置为 00:00:00:
然后您可以比较日期值。请参阅参考文档中的概述< /a>.
You set the time in the date to 00:00:00 before doing the comparison:
Then you can compare the date values. See the overview in reference documentation.
iOS 8 中向 NSCalendar 引入了一种新方法,使这一切变得更加容易。
您可以将粒度设置为重要的单位。这会忽略所有其他单位并限制与所选单位的比较。
There's a new method that was introduced to NSCalendar with iOS 8 that makes this much easier.
You set the granularity to the unit(s) that matter. This disregards all other units and limits comparison to the ones selected.
对于 iOS8 及更高版本,检查两个日期是否在同一天很简单:
请参阅 文档
For iOS8 and later, checking if two dates occur on the same day is as simple as:
See documentation
这是所有答案的简写:
This is a shorthand of all the answers:
我使用了 Duncan C 的方法,我修正了他犯的一些错误
I used the Duncan C approach, I have fixed some mistakes he made
我使用这个小 util 方法:(
你显然需要有一个名为
calendar_
的 ivar,其中包含一个NSCalendar
。)使用这个方法,可以很容易地检查日期是否像今天这样this:
(
[NSDate date]
返回当前日期和时间。)这当然与 Gregory 的建议非常相似。这种方法的缺点是它往往会创建大量临时的 NSDate 对象。如果您要处理大量日期,我建议使用其他方法,例如直接比较组件,或使用
NSDateComponents
对象而不是NSDates
。I use this little util method:
(You obviously need to have an ivar called
calendar_
containing anNSCalendar
.)Using this, it is easy to check if a date is today like this:
(
[NSDate date]
returns the current date and time.)This is of course very similar to what Gregory suggests. The drawback of this approach is that it tends to create lots of temporary
NSDate
objects. If you're going to process a lot of dates, I would recommend using some other method, such as comparing the components directly, or working withNSDateComponents
objects instead ofNSDates
.答案比每个人想象的都要简单。 NSCalendar 有一个方法,
该方法允许您使用您想要的任何单位计算两个日期之间的差异。
所以写一个这样的方法:
如果上面的方法返回零,则两个日期是同一天。
The answer is simpler than everybody makes it out to be. NSCalendar has a method
That method lets you calculate the difference between two dates using whatever units you want.
So write a method like this:
If the above method returns zero, the two dates are on the same day.
从 iOS 8.0 开始,您可以使用:
如果结果是例如 NSOrderedDescending,则 otherDate 早于 [NSDate date] 天数。
我在 NSCalendar 文档中没有看到此方法,但它位于 iOS 7.1 到 iOS 8.0 API 差异
From iOS 8.0 onwards, you can use:
If the result is e.g. NSOrderedDescending, otherDate is before [NSDate date] in terms of days.
I do not see this method in the NSCalendar documentation but it is in the iOS 7.1 to iOS 8.0 API Differences
对于使用 Swift 3 进行编码的开发人员
For developers coding in Swift 3
使用 Swift 3,根据您的需要,您可以选择以下两种模式之一来解决您的问题。
#1.使用
compare(_:to:toGranularity:)
方法Calendar
有一个名为比较(_:到:到粒度:)
。compare(_: to: to Granularity: )
具有以下声明:下面的 Playground 代码显示了它的热门用法:
#2。使用
dateComponents(_:from:to:)
Calendar
有一个名为dateComponents(_:from:to:)
。dateComponents(_:from:to:)
具有以下声明:下面的 Playground 代码显示了如何使用它:
With Swift 3, according to your needs, you can choose one of the two following patterns in order to solve your problem.
#1. Using
compare(_:to:toGranularity:)
methodCalendar
has a method calledcompare(_:to:toGranularity:)
.compare(_:to:toGranularity:)
has the following declaration:The Playground code below shows hot to use it:
#2. Using
dateComponents(_:from:to:)
Calendar
has a method calleddateComponents(_:from:to:)
.dateComponents(_:from:to:)
has the following declaration:The Playground code below shows hot to use it:
我的解决方案是使用 NSDateFormatter 进行两次转换:
my solution was two conversions with NSDateFormatter:
有关 NSDate< 的文档/a> 表示
compare:
和isEqual:
方法都将执行基本比较并对结果进行排序,尽管它们仍然考虑时间。管理任务的最简单方法可能是创建一个新的 isToday 方法,其效果如下:
The documentation regarding NSDate indicates that the
compare:
andisEqual:
methods will both perform a basic comparison and order the results, albeit they still factor in time.Probably the simplest way to manage the task would be to create a new
isToday
method to the effect of the following:这是一只特别丑陋的猫,但这里有另一种方法。我并不是说它很优雅,但它可能与 iOS 中的日期/时间支持最接近。
This is a particularly ugly cat to skin, but here's another way to do it. I don't say it's elegant, but it's probably as close as you can get with the date/time support in iOS.
比较小时以及修复 1 天的差异
compare hour as well to fix 1day difference
令我惊讶的是,没有其他答案有此选项来获取对象的“一天的开始”日期:
它将
date1
和date2
设置为各自日期的开始。如果相等,则它们在同一天。或者这个选项:
将
day1
和day2
设置为可以比较的任意值。如果相等,则它们在同一天。I'm surprised that no other answers have this option for getting the "beginning of day" date for the objects:
Which sets
date1
anddate2
to the beginning of their respective days. If they are equal, they are on the same day.Or this option:
Which sets
day1
andday2
to somewhat arbitrary values that can be compared. If they are equal, they are on the same day.