当前位置 : 主页 > 手机开发 > 其它 >

swift – 比较NSDate

来源:互联网 收集:自由互联 发布时间:2021-06-11
我想比较两个NSDates,但每个日期显示为“早于”今天的日期.有任何想法吗? let compareResult = self.todaysDate.compare(self.date)if compareResult == NSComparisonResult.OrderedDescending {println("Today is later than
我想比较两个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.hour
let minutes = components.minute
let month = components.month
let year = components.year
let day = components.day
println(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 < tomorrow
assert(today.compare(yesterday) == .OrderedDescending)    // today > yesterday
assert(today.compare(today)     == .OrderedSame)          // today == today
网友评论