转换后得到不正确的日期格式
Getting incorrect date format after converting
我从 API 中获取字符串格式的日期:“2020-01-02T00:00:00”。
现在我想把这个日期转换成Date
格式。所以这就是我为此所做的...
var utcTime = "\(dic["Due_Date"]!)" //Printing `utcTime` gives "2020-01-02T00:00:00"
self.dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
self.dateFormatter.locale = Locale(identifier: "en_US")
if let date = dateFormatter.date(from:utcTime) {
self.scheduledDate = date //HERE I GET THE DATE AS 2020-01-01 18:30:00 UTC
}
收到的字符串格式日期为“2020-01-02T00:00:00”。但是当我将它转换为 Date
格式时,我得到的日期是 2020-01-01 18:30:00 UTC
,这是不正确的。
您还需要设置时区。
let utcTime = "\(dic["Due_Date"]!)"
let dateFormatter = DateFormatter()
let timezone = TimeZone.init(secondsFromGMT: 0)
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
dateFormatter.timeZone = timezone!
if let date = dateFormatter.date(from:utcTime) {
print(date) // 2020-01-02 00:00:00 +0000
}
let inputDate = "2020-01-02T00:00:00"
let dateFmt = DateFormatter()
dateFmt.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
if let date = dateFmt.date(from: inputDate) {
//IF YOU NEED TO CHANGE DATE FORMATE
dateFmt.dateFormat = "dd-MMM-yyyy"
print(dateFmt.string(from: date))
}
您必须将时区设置为 UTC(协调世界时)
var utcTime = "2020-01-02T00:00:00" //Printing `utcTime` gives "2020-01-02T00:00:00"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
dateFormatter.locale = Locale(identifier: "en_US")
dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
if let date = dateFormatter.date(from:utcTime) {
print(date) //HERE I GET THE DATE AS 2020-01-01 18:30:00 UTC
}
输出:-
我从 API 中获取字符串格式的日期:“2020-01-02T00:00:00”。
现在我想把这个日期转换成Date
格式。所以这就是我为此所做的...
var utcTime = "\(dic["Due_Date"]!)" //Printing `utcTime` gives "2020-01-02T00:00:00"
self.dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
self.dateFormatter.locale = Locale(identifier: "en_US")
if let date = dateFormatter.date(from:utcTime) {
self.scheduledDate = date //HERE I GET THE DATE AS 2020-01-01 18:30:00 UTC
}
收到的字符串格式日期为“2020-01-02T00:00:00”。但是当我将它转换为 Date
格式时,我得到的日期是 2020-01-01 18:30:00 UTC
,这是不正确的。
您还需要设置时区。
let utcTime = "\(dic["Due_Date"]!)"
let dateFormatter = DateFormatter()
let timezone = TimeZone.init(secondsFromGMT: 0)
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
dateFormatter.timeZone = timezone!
if let date = dateFormatter.date(from:utcTime) {
print(date) // 2020-01-02 00:00:00 +0000
}
let inputDate = "2020-01-02T00:00:00"
let dateFmt = DateFormatter()
dateFmt.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
if let date = dateFmt.date(from: inputDate) {
//IF YOU NEED TO CHANGE DATE FORMATE
dateFmt.dateFormat = "dd-MMM-yyyy"
print(dateFmt.string(from: date))
}
您必须将时区设置为 UTC(协调世界时)
var utcTime = "2020-01-02T00:00:00" //Printing `utcTime` gives "2020-01-02T00:00:00"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
dateFormatter.locale = Locale(identifier: "en_US")
dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
if let date = dateFormatter.date(from:utcTime) {
print(date) //HERE I GET THE DATE AS 2020-01-01 18:30:00 UTC
}
输出:-