//时间戳转成字符串
func timeIntervalChangeToTimeStr(timeInterval:TimeInterval, dateFormat:String?) -> String {
let date:NSDate = NSDate.init(timeIntervalSince1970: timeInterval/1000)
let formatter = DateFormatter.init()
if dateFormat == nil {
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
}else{
formatter.dateFormat = dateFormat
}
return formatter.string(from: date as Date)
}
//字符串转时间戳
func timeStrChangeTotimeInterval(timeStr: String?, dateFormat:String?) -> String {
if timeStr?.count ?? 0 > 0 {
return ""
}
let format = DateFormatter.init()
format.dateStyle = .medium
format.timeStyle = .short
if dateFormat == nil {
format.dateFormat = "yyyy-MM-dd HH:mm:ss"
}else{
format.dateFormat = dateFormat
}
let date = format.date(from: timeStr!)
return String(date!.timeIntervalSince1970)
}
【时间戳和字符串之间转换 Swift】
推荐阅读
- OC编程|OC Swift NotificationCenter通知
- Swift 字典 Dictionary基本用法
- swift|swift 获取当前viewcontroller (rootVC)
- swiftc
- Other|全国银行SWIFT代码查询
- Swift|以太坊Web3Swift常用方法使用
- ios|CocoaPods pod install / pod update 更新太慢 卡死 问题解决