//获取当前距1970时间戳 func getNowTimestamp() -> Int{ let date = NSDate.init() return Int(date.timeIntervalSince1970) } /* *获取指定距1970时间戳 格式: "yyyy-MM-dd HH:mm:ss" */ func getOtherTimestamp(timesDateString:String) ->Int{ let df = DateFormatter.init() df.dateFormat = "yyyy-MM-dd HH:mm:ss" let ds = df.date(from: timesDateString) return Int((ds?.timeIntervalSince1970)!) } func test(){ //两个时间对比 print(getNowTimestamp() - getOtherTimestamp(timesDateString: "2017-07-29 14:20:01")) }