将日期格式 is-8601 更改为自定义格式
change date format iso-8601 to custom format
我有一个 json 文件正在使用 JSONDecoder() 进行解析。但是,我收到了 iso-8601 格式 ("yyyy-MM-dd'T'HH:mm:ss.SSSXXXXX") 的 Date 类型的变量时间戳,但在我看来,我想以自定义格式显示它:"dd/mm/yy HH:mm:ss".
我写了下面的代码,但是我得到的时间戳为零,而且我认为当时间戳以 iso-8601 格式出现时 "date" 不是正确的类型:
Error json: typeMismatch(Swift.Double,
Swift.DecodingError.Context(codingPath: [_JSONKey(stringValue: "Index
0", intValue: 0), CodingKeys(stringValue: "timestamp", intValue:
nil)], debugDescription: "Expected to decode Double but found a
string/data instead.", underlyingError: nil))
swift4
import UIKit
enum Type : String, Codable {
case organizational, planning
}
// structure from json file
struct News: Codable{
let type: Type
let timestamp: Date //comes in json with ISO-8601-format
let title: String
let message: String
enum CodingKeys: String, CodingKey { case type, timestamp, title, message}
let dateFormatter : DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "dd/MM/yy HH:mm:ss" // change format ISO-8601 to dd/MM/yy HH:mm:ss
return formatter
}()
var dateString : String {
return dateFormatter.string(from:timestamp) // take timestamp variable of type date and make it a string -> lable.text
}
}
当您解码 Date
时,解码器默认需要一个 UNIX 时间戳(Double
),这就是错误消息告诉您的内容。
但是,如果添加 decoder.dateDecodingStrategy = .iso8601
,您确实可以将 ISO8601 字符串解码为 Date
,但这只会解码标准的 ISO8601 字符串 ,而无需 毫秒。
有两种选择:
添加 formatted
dateDecodingStrategy
和 DateFormatter
.
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSXXXXX"
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .formatted(dateFormatter)
try decoder.decode(...
声明timestamp
为
let timestamp: String
并在 dateString
.
中使用两个格式化程序或两个日期格式来回转换字符串
我有一个 json 文件正在使用 JSONDecoder() 进行解析。但是,我收到了 iso-8601 格式 ("yyyy-MM-dd'T'HH:mm:ss.SSSXXXXX") 的 Date 类型的变量时间戳,但在我看来,我想以自定义格式显示它:"dd/mm/yy HH:mm:ss".
我写了下面的代码,但是我得到的时间戳为零,而且我认为当时间戳以 iso-8601 格式出现时 "date" 不是正确的类型:
Error json: typeMismatch(Swift.Double, Swift.DecodingError.Context(codingPath: [_JSONKey(stringValue: "Index 0", intValue: 0), CodingKeys(stringValue: "timestamp", intValue: nil)], debugDescription: "Expected to decode Double but found a string/data instead.", underlyingError: nil))
swift4
import UIKit
enum Type : String, Codable {
case organizational, planning
}
// structure from json file
struct News: Codable{
let type: Type
let timestamp: Date //comes in json with ISO-8601-format
let title: String
let message: String
enum CodingKeys: String, CodingKey { case type, timestamp, title, message}
let dateFormatter : DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "dd/MM/yy HH:mm:ss" // change format ISO-8601 to dd/MM/yy HH:mm:ss
return formatter
}()
var dateString : String {
return dateFormatter.string(from:timestamp) // take timestamp variable of type date and make it a string -> lable.text
}
}
当您解码 Date
时,解码器默认需要一个 UNIX 时间戳(Double
),这就是错误消息告诉您的内容。
但是,如果添加 decoder.dateDecodingStrategy = .iso8601
,您确实可以将 ISO8601 字符串解码为 Date
,但这只会解码标准的 ISO8601 字符串 ,而无需 毫秒。
有两种选择:
添加
formatted
dateDecodingStrategy
和DateFormatter
.let dateFormatter = DateFormatter() dateFormatter.locale = Locale(identifier: "en_US_POSIX") dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSXXXXX" let decoder = JSONDecoder() decoder.dateDecodingStrategy = .formatted(dateFormatter) try decoder.decode(...
声明
timestamp
为let timestamp: String
并在
dateString
. 中使用两个格式化程序或两个日期格式来回转换字符串