Swift擴充套件Date
阿新 • • 發佈:2018-12-17
extension Date { static func getCurrentTime() -> 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) } }