首页 > 解决方案 > Ios Swift 日期从 TimeZone 转换

问题描述

 dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
    dateFormatter.timeZone = TimeZone(abbreviation: "KST")
    let nowstring = dateFormatter.string(from: Date())
    let nowdate = dateFormatter.date(from: nowstring)
    print(nowstring)
    print(nowdate)

打印这个2018-07-24T15:06:26 Optional(2018-07-24 06:06:26 +0000)

我想要现在日期值2018-07-24T15:06:26

但是没有时区的字符串到日期

我必须使用,timeIntervalSince所以 nowdate 必须有时区

标签: iosswiftdate

解决方案


let today = NSDate()
print("\(today)")


let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let nowDatestring = formatter.string(from: today)

formatter.timeZone = TimeZone(abbreviation: "KST")
let kstDate = formatter.date(from: nowDatestring)
print("\(kstDate!)")


formatter.timeZone = TimeZone(abbreviation: "PST")
let pstDate = formatter.date(from: nowDatestring)
print("\(pstDate!)")


formatter.timeZone = TimeZone(abbreviation: "IST")
let istDate = formatter.date(from: nowDatestring)
print("\(istDate!)")

推荐阅读