获取当前时间 时间戳/字符串
时间戳和字符串相互转化
import Foundation
extension Date {
static func currentTimeInterval() -> Int64 {
let now = Date()
let timeInterval: TimeInterval = now.timeIntervalSince1970
return Int64(timeInterval)
}
static func currentTime() -> String {
let date = Date()
let timeFormatter = DateFormatter()
timeFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
return timeFormatter.string(from: date)
}
static func formatterTimeToDate(stringTime: String) -> Date {
let dfmatter = DateFormatter()
dfmatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
dfmatter.timeZone = TimeZone(abbreviation: "UTC") ?? TimeZone.current
let date = dfmatter.date(from: stringTime) ?? Date()
return date
}
static func timeStampToString(timeStamp: String) -> String {
let timeInterval: TimeInterval = TimeInterval(timeStamp) ?? Date().timeIntervalSince1970
let dfmatter = DateFormatter()
dfmatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let date = Date(timeIntervalSince1970: timeInterval)
return dfmatter.string(from: date)
}
}