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所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)