如何判断 NSDate 是否是今天?
如何检查 NSDate
是否属于今天?
我曾经使用 [aDate description]
中的前 10 个字符进行检查。 [[aDate description] substringToIndex:10]
返回类似 "YYYY-MM-DD"
的字符串,因此我将该字符串与 [[[NSDate 返回的字符串进行比较日期] 描述] substringToIndex:10]
。
有没有更快速和/或更简洁的方法来检查?
谢谢。
How to check if an NSDate
belongs to today?
I used to check it using first 10 characters from [aDate description]
. [[aDate description] substringToIndex:10]
returns string like "YYYY-MM-DD"
so I compared the string with the string returned by [[[NSDate date] description] substringToIndex:10]
.
Is there more fast and/or neat way to check?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(20)
在 macOS 10.9+ 中iOS 8+,NSCalendar/Calendar 上有一个方法可以做到这一点!
所以你只需执行
Objective-C:
Swift 3:
In macOS 10.9+ & iOS 8+, there's a method on NSCalendar/Calendar that does exactly this!
So you'd simply do
Objective-C:
Swift 3:
您可以比较日期组件:
编辑:
我更喜欢 stefan 的方法,我认为它可以使 if 语句更清晰、更易于理解:
克里斯,我已经采纳了您的建议。我必须查一下时代是什么,所以对于其他不知道的人来说,它区分了公元前和公元。对于大多数人来说这可能是不必要的,但它很容易检查并增加了一些确定性,所以我将其包括在内。如果您追求速度,这可能不是一个好方法。
注意与许多关于SO的答案一样,7年后这已经完全过时了。在 Swift 中现在只需使用
.isDateInToday
You can compare date components:
Edit:
I like stefan's method more, I think it makes for a cleaner and more understandable if statement:
Chris, I've incorporated your suggestion. I had to look up what era was, so for anyone else who doesn't know, it distinguishes between BC and AD. This is probably unnecessary for most people, but it's easy to check and adds some certainty, so I've included it. If you're going for speed, this probably isn't a good method anyway.
NOTE as with many answers on SO, after 7 years this is totally out of date. In Swift now just use
.isDateInToday
这是您问题的一个分支,但如果您想打印带有“今天”或“昨天”的 NSDate,请使用
NSDateFormatter 的函数
This is an offshoot to your question, but if you want to print an NSDate with "Today" or "Yesterday", use the function
for NSDateFormatter
我会尝试将今天的日期标准化为午夜,然后将第二个日期标准化为午夜,然后比较它是否是相同的 NSDate。
来自 Apple 示例 以下是如何将今天的日期标准化为午夜,对第二个日期执行相同的操作并进行比较:
I would try to get today's date normalized to midnight and the second date, normalize to midnight then compare if it is the same NSDate.
From an Apple example here's how you normalize to midnight today's date, do the same for the second date and compare:
Catfish_Man 建议的工作Swift 扩展:
Working Swift extension of the suggestion by Catfish_Man:
无需处理组件、时代和其他东西。
NSCalendar 提供了一种方法来获取现有日期的特定时间单位的开始时间。
此代码将获取今天和另一个日期的开始并进行比较。如果其计算结果为
NSOrderedSame
,则两个日期都在同一天 - 所以今天。No need to juggle with components, eras and stuff.
NSCalendar provides an method to get the beginning of a certain time unit for an existing date.
This code will get the begin of today and another date and compare that. If it evaluates to
NSOrderedSame
, both dates are during the same day — so today.在 Swift 2.0 上为我工作
Worked for me on Swift 2.0
最佳答案的 Swift 版本:
Swift version of the best answer:
请参阅标题为“执行日历计算”的 Apple 文档条目 [链接]。
该页面上的清单 13 建议,要确定几天之间的午夜数,您可以使用:
然后,您可以使用该方法并查看它是否返回 0 来确定两天是否相同。
Refer to Apple's documentation entry entitled "Performing Calendar Calculations" [link].
Listing 13 on that page suggests that to determine the number of midnights between days, you use:
You may then determine if two days are the same by using that method and seeing if it returns 0 or not.
您还可以检查您的日期和当前日期之间的时间间隔:
这将为您提供 myDate 和当前日期/时间之间的时间间隔(以秒为单位)。
链接。
编辑:请注意:我很清楚 [myDate timeIntervalSinceNow] 并不能明确确定 myDate 是否是今天。
我保留这个答案,这样如果有人正在寻找类似的东西并且 [myDate timeIntervalSinceNow] 有用,他们可能会在这里找到它。
You could also check the time interval between the date you have, and the current date:
This will give you the time interval, in seconds, between myDate and the current date/time.
Link.
Edit: Note to everyone: I'm well aware that [myDate timeIntervalSinceNow] does not unambiguously determine whether myDate is today.
I am leaving this answer as is so that if someone is looking for something similar and [myDate timeIntervalSinceNow] is useful, they may find it here.
基于最佳答案的 Swift 扩展:
Swift Extension based on the best answers:
如果您有很多这样的日期比较,那么对
calendar:components:fromDate
的调用就会开始占用大量时间。根据我所做的一些分析,它们似乎相当昂贵。假设您正在尝试确定某个日期数组中的哪些日期(例如
NSArray *datesToCompare
)与某个给定日期(例如NSDate *baseDate
)是同一天,那么您可以使用类似以下内容(部分改编自上面的答案):输出:
If you have a lot of these date comparisons, then the calls to
calendar:components:fromDate
start to take up a lot of time. According to some profiling I have done, they seem to be quite expensive.Say you are trying to determine which from some array of dates, say
NSArray *datesToCompare
, are the same day as some given day, sayNSDate *baseDate
, then you can use something like the following (partly adapted from an answer above):Output:
有一个比上述许多答案更简单的方法!
There is an easier way than many of the above answers!
这可能会被重新设计为 NSDate 类别,但我使用了:(
但是,比较原始问题中的两个日期字符串几乎感觉“更干净”..)
This could probably be reworked as an NSDate category, but i used:
(However, comparing the two date strings as in the original question almost feels 'cleaner'..)
对于 iOS7 及更早版本:
for iOS7 and earlier:
查看我们的 Erica Sadun 出色的
NSDate 扩展
。使用起来非常简单。在这里进行修改:http://github.com/erica/NSDate-Extensions
它已经在那里了在这篇文章中:https://stackoverflow.com/a/4052798/362310
Check our Erica Sadun's great
NSDate extension
. Very simple to use. Fine it here:http://github.com/erica/NSDate-Extensions
It's already there in this post: https://stackoverflow.com/a/4052798/362310
正确且安全的解决方案,无需强制展开,适用于 Swift 2.2 和 iOS 8 之前的版本:
The correct and safe solution without force-unwrapping, working on Swift 2.2 and before iOS 8:
这是我的 2 美分答案,它建立在已接受的答案的基础上,但也支持更新的 API。注意:我使用公历,因为大多数时间戳都是 GMT,但可以根据需要更改您的时间戳
Here's my 2 cent answer building on the accepted answer but supporting the newer API as well. Note: I use the Gregorian calendar as most time stamps are GMT but change yours as you see fit
我的解决方案是通过除法计算自 1970 年以来过去了多少天并比较整数部分
My solution is calculate how much days passed since 1970 by division and compare the integer part