swift – 比较NSDate

swift – 比较NSDate,第1张

概述我想比较两个NSDates,但每个日期显示为“早于”今天的日期.有任何想法吗? let compareResult = self.todaysDate.compare(self.date)if compareResult == NSComparisonResult.OrderedDescending {println("Today is later than date2")} else { 我想比较两个NSDates,但每个日期显示为“早于”今天的日期.有任何想法吗?

let compareResult = self.todaysDate.compare(self.date)if compareResult == NSComparisonResult.OrderedDescending {println("Today is later than date2")} else {println("Future")}

获得“今日日期”

let todaysDate = NSDate()let calendar = NSCalendar.currentCalendar()    let components = calendar.components(.CalendarUnitHour | .CalendarUnitMinute | .CalendarUnitMonth | .CalendarUnitYear | .CalendarUnitDay,fromDate: todaysDate)let hour = components.hourlet minutes = components.minutelet month = components.monthlet year = components.yearlet day = components.dayprintln(todaysDate)

这个印刷品是:

2014-11-12 14:48:48 +0000

而“日期”的印刷品是:

2014-10-24 07:24:41 +0000

这是在Parse.com服务器上.

谢谢

解决方法 我想也许你正在解释比较倒退的结果.看一下这个.每个断言通过:

let today = NSDate()let tomorrow = today.dateByAddingTimeInterval(24 * 60 * 60)let yesterday = today.dateByAddingTimeInterval(-24 * 60 * 60)assert(today.compare(tomorrow)  == .OrderedAscending)     // today < tomorrowassert(today.compare(yesterday) == .OrderedDescending)    // today > yesterdayassert(today.compare(today)     == .OrderedSame)          // today == today
总结

以上是内存溢出为你收集整理的swift – 比较NSDate全部内容,希望文章能够帮你解决swift – 比较NSDate所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/999922.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存