有两个NSDate变量,需要判断时间大小:
var timestamp:NSDate if newMessage.timestamp > curMessage.timestamp { |
搜:
swift nsdate compare
NSDate Comparison using Swift – Stack Overflow
代码:
//: Playground – noun: a place where people can play import UIKitlet timestamp1IntInMsec = 1449805150184 let timestamp2IntInMsec = 1449605150184 let timestamp3IntInMsec = 1449605150184func parseDateFromTimestamp(timestampIntInMsec:Int) -> NSDate { print("timestampIntInMsec=\(timestampIntInMsec)") let timestampDoubleInSec:Double = Double(timestampIntInMsec)/1000 print("timestampDoubleInSec=\(timestampDoubleInSec)") let parsedDate:NSDate = NSDate(timeIntervalSince1970: NSTimeInterval(timestampDoubleInSec)) print("parsedDate=\(parsedDate)") return parsedDate }let timestamp1 = parseDateFromTimestamp(timestamp1IntInMsec) let timestamp2 = parseDateFromTimestamp(timestamp2IntInMsec) let timestamp3 = parseDateFromTimestamp(timestamp3IntInMsec)public func ==(lhs: NSDate, rhs: NSDate) -> Bool { return lhs === rhs || lhs.compare(rhs) == .OrderedSame }public func <(lhs: NSDate, rhs: NSDate) -> Bool { return lhs.compare(rhs) == .OrderedAscending }extension NSDate: Comparable { }timestamp1 > timestamp2 timestamp1 < timestamp2timestamp1 == timestamp2 timestamp3 == timestamp2timestamp1 <= timestamp2 timestamp1 >= timestamp2timestamp3 <= timestamp2 timestamp3 >= timestamp2 |
效果:
转载请注明:在路上 » [已解决]swift中判断两个NSDate的时间大小