如何比较两个日期?
我有三个日期:(1) previousDate (2) currentDate (3) nextDate, 我想检查 currentDate 是否晚于前一个日期且早于 nextDate。 我该怎么做?
I have three dates: (1) previousDate (2) currentDate (3) nextDate,
I want to check whether currentDate is later then previous date and earlier than nextDate.
How do I do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我想您正在使用 NSDate 类。
您可以使用 isEqualToDate 比较两个 NSDate 对象。还有 earlierDate 和 laterDate 检查当前日期是否大于上一个日期且小于下一个日期。
I suppose you're using the NSDate class.
You can use isEqualToDate to compare two NSDate objects. And also the earlierDate and laterDate to check currentdate is bigger than previous date and smaller than next date.
我只是用它来检查一下:
对我来说效果很好!谢谢@Luca Matteis 的提示“稍后日期:”
I simply used this to check it:
worked fine for me! Thx @Luca Matteis for the hint "laterDate:"
我们可以在 swift 5.0 中轻松比较两个日期
We can compare two dates easily in swift 5.0
NSDate 对象还实现了有据可查的 -compare: 方法。
NSDate objects also implement well-documented -compare: method.
只需使用
NSDate
的compare
消息执行两次比较:Just perform two comparisons using the
compare
message ofNSDate
: