我有一个像 2017-08-12 11:30:00 这样的日期,我想将日期显示为
2017 年 8 月 12 日星期六上午 11:30(格林威治标准时间 06:00)
我得到 2017 年 8 月 12 日星期六上午 11:30
使用以下代码
let myDate = "2017-08-12 11:30:00"
let date = myDate.date(format: .custom("yyyy-MM-dd HH:mm:ss"))
let strDate = date?.string(format: .custom("EEE, dd MMM yyyy hh:mm a"))
let localDate = strDate?.date(format: .custom("EEE, dd MMM yyyy hh:mm a"))
let strLocalDate = localDate?.string(format: .custom("EEE, dd MMM yyyy hh:mm a"))
但是我怎样才能找到 GMT 呢?
对于 GMT,我也尝试过 formatter.timeZone = TimeZone(identifier:"GMT")
但它没有用。
请帮忙, 谢谢!
最佳答案
试试这段代码。我已经测试过,它在 swift3.0 中对我有用。
let myDate = "2017-08-12 11:30:00"
let localDateFormat = DateFormatter()
localDateFormat.dateFormat = "YYYY-mm-dd hh:mm:ss"
let localDate = localDateFormat.date(from: myDate)
localDateFormat.dateFormat = "EEE, dd MMM yyyy hh:mm a"
let myNewDate = localDateFormat.string(from: localDate!)
print(myNewDate)
//GMT Date
let gmtDateFormat = DateFormatter()
gmtDateFormat.dateFormat = "HH:mm a"
gmtDateFormat.timeZone = NSTimeZone(abbreviation: "GMT")! as TimeZone
let gmtDateString = gmtDateFormater.string(from: localDate!)
print(gmtDateString)
print("time:\(myNewDate)(\(gmtDateString))")
关于ios - 我怎样才能找到给定日期的格林威治标准时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46296642/