swift 时间戳转Date

    func stringConvertDate(string:String, dateFormat:String="yyyy-MM-dd HH:mm:ss") -> Date {
        let timeZone = TimeZone.init(identifier: "UTC") //这是重点
        let dateFormatter = DateFormatter.init()
        dateFormatter.timeZone = timeZone
        dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
        let date = dateFormatter.date(from: string)
        print("stringConvertDate = \(String(describing: date))")
        return date!
    }
    func timeStampToString(timeStamp: Double)->String {
        //时间戳为毫秒级要 / 1000, 秒就不用除1000,参数带没带000
        let timeSta:TimeInterval = TimeInterval(timeStamp / 1000)
        let date = NSDate(timeIntervalSince1970: timeSta)
        let dateformatter = DateFormatter()
        //yyyy-MM-dd HH:mm:ss
        dateformatter.dateFormat="yyyy-MM-dd HH:mm:ss"
        let date1 = dateformatter.string(from: date as Date)
        print("timeStampToString = \(date1)")
        return dateformatter.string(from: date as Date)
    }

你可能感兴趣的:(swift 时间戳转Date)