最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]swift中判断两个NSDate的时间大小

Swift crifan 3287浏览 0评论

有两个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 UIKit

let timestamp1IntInMsec = 1449805150184
let timestamp2IntInMsec = 1449605150184
let timestamp3IntInMsec = 1449605150184

func 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 < timestamp2

timestamp1 == timestamp2
timestamp3 == timestamp2

timestamp1 <= timestamp2
timestamp1 >= timestamp2

timestamp3 <= timestamp2

timestamp3 >= timestamp2

效果:

转载请注明:在路上 » [已解决]swift中判断两个NSDate的时间大小

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
84 queries in 0.178 seconds, using 22.08MB memory