最新消息:服务器现已从linode换到fzhost.net,且主题已从retina 0.2换为大前端D8

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

Swift crifan 34浏览 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,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
51 queries in 0.514 seconds, using 12.89MB memory