检查指定日期是今天、昨天还是将来的日期
我有一个关于 NSDate 的疑问。我有一个日期,即“2011-10-04 07:36:38 +0000”,我想检查这个日期是昨天、今天还是未来的日期。
我该怎么办呢?
I have one query regarding NSDate. I have a date i.e. "2011-10-04 07:36:38 +0000", and I want to check if this date is yesterday, or today or a future date.
How would I go about this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
试试这个:
注意:根据您的需要更改日期格式。
Try this:
Note: Change the date format as per your need.
请参阅 Apple 关于日期计算的文档:
如果
days
介于 +1 和-1 那么你的约会对象就是“今天”的候选者。显然,您需要考虑如何处理时间。想必最简单的方法是将所有日期设置为相关日期的 00:00.00 小时(截断使用这样的方法计算日期),然后使用这些值进行计算。这样你今天会得到 0,昨天会得到 -1,明天会得到 +1,任何其他值同样会告诉你事情在未来或过去有多远。See Apple's documentation on date calculations:
If
days
is between +1 and -1 then your date is a candidate for being "today". Obviously you'll need to think about how you handle hours. Presumably the easiest thing would be to set all dates to be 00:00.00 hours on the day in question (truncate the date using an approach like this), and then use those values for the calculation. That way you'd get 0 for today, -1 for yesterday, +1 for tomorrow, and any other value would likewise tell you how far things were in the future or the past.根据您的需要使用以下任何内容,
请参阅此 http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDate_Class/Reference/Reference.html
Use any of the folowing according to ur need,
Refer this http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDate_Class/Reference/Reference.html