此日期时间响应的正确格式是什么
What is the correct format for this date time response
我正在尝试使用 codable
将 json 响应解码为模型,但在解析日期时遇到问题。
我在 DateFormatter
中使用的格式是 "yyyy-MM-dd'T'HH:mmZ"
和数据样本
{
"startDate": "2019-01-27T00:00:00",
"endDate": "2019-02-02T00:00:00",
"id": null
}, {
"startDate": "2019-01-20T00:00:00",
"endDate": "2019-01-26T00:00:00",
"id": null
}, {
"startDate": "2019-01-13T00:00:00",
"endDate": "2019-01-19T00:00:00",
"id": null
}, {
"startDate": "2019-01-13T00:00:00",
"endDate": "2020-01-19T00:00:00",
"id": null
}, {
"startDate": "2019-01-06T00:00:00",
"endDate": "2019-01-12T00:00:00",
"id": null
}
我还尝试了 "yyyy-MM-dd'T'HH:mmZ"
、"yyyy-MM-dd'T'HH:mm:ssZ"
和 "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
,在 T
周围使用和不使用 '
我收到的错误是
"NSCodingPath": [CodingKeys(stringValue: "result", intValue: nil), _JSONKey(stringValue: "Index 0", intValue: 0), CodingKeys(stringValue: "startDate", intValue: nil)], "NSDebugDescription": "Date string does not match format expected by formatter."
我确定我的格式正确,但那个错误让我怀疑我的理智。
得到格式
不需要 Z
格式说明符,因为日期字符串不包含时区偏移量。
是yyyy-MM-dd'T'HH:mm:ss
。您的字符串有秒(因此包括 :ss
),但没有毫秒(即不包括 .SSS
)或时区限定符(即不包括 Z
)。
对我来说有趣的问题是它应该采用哪个时区。默认情况下,在字符串中没有时区部分的情况下,DateFormatter
将采用本地时区,即使这些时区很有可能是 GMT/UTC/Zulu。如果是这样,请显式设置格式化程序的 timeZone
。当然,不要忘记设置 locale
.
let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.timeZone = TimeZone(secondsFromGMT: 0) // include this only if dates are GMT/UTC/Zulu
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
顺便说一下,如果你愿意,你也可以使用 ISO8601DateFormatter
,假设 UTC/GMT,如果你愿意:
let formatter = ISO8601DateFormatter()
formatter.formatOptions = [.withFullDate, .withTime, .withColonSeparatorInTime]
而且,在那种情况下,如果您想使用本地时区,则必须指定它:
formatter.timeZone = .current
很遗憾,dateDecodingStrategy
不能使用 ISO8601DateFormatter
。同样,.iso8601
策略假设您将拥有时区字符。
因此,对于 Codable
和您的特定字符串,您必须坚持上面的 DateFormatter
示例。
我正在尝试使用 codable
将 json 响应解码为模型,但在解析日期时遇到问题。
我在 DateFormatter
中使用的格式是 "yyyy-MM-dd'T'HH:mmZ"
和数据样本
{
"startDate": "2019-01-27T00:00:00",
"endDate": "2019-02-02T00:00:00",
"id": null
}, {
"startDate": "2019-01-20T00:00:00",
"endDate": "2019-01-26T00:00:00",
"id": null
}, {
"startDate": "2019-01-13T00:00:00",
"endDate": "2019-01-19T00:00:00",
"id": null
}, {
"startDate": "2019-01-13T00:00:00",
"endDate": "2020-01-19T00:00:00",
"id": null
}, {
"startDate": "2019-01-06T00:00:00",
"endDate": "2019-01-12T00:00:00",
"id": null
}
我还尝试了 "yyyy-MM-dd'T'HH:mmZ"
、"yyyy-MM-dd'T'HH:mm:ssZ"
和 "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
,在 T
'
我收到的错误是
"NSCodingPath": [CodingKeys(stringValue: "result", intValue: nil), _JSONKey(stringValue: "Index 0", intValue: 0), CodingKeys(stringValue: "startDate", intValue: nil)], "NSDebugDescription": "Date string does not match format expected by formatter."
我确定我的格式正确,但那个错误让我怀疑我的理智。
得到格式不需要 Z
格式说明符,因为日期字符串不包含时区偏移量。
是yyyy-MM-dd'T'HH:mm:ss
。您的字符串有秒(因此包括 :ss
),但没有毫秒(即不包括 .SSS
)或时区限定符(即不包括 Z
)。
对我来说有趣的问题是它应该采用哪个时区。默认情况下,在字符串中没有时区部分的情况下,DateFormatter
将采用本地时区,即使这些时区很有可能是 GMT/UTC/Zulu。如果是这样,请显式设置格式化程序的 timeZone
。当然,不要忘记设置 locale
.
let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.timeZone = TimeZone(secondsFromGMT: 0) // include this only if dates are GMT/UTC/Zulu
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
顺便说一下,如果你愿意,你也可以使用 ISO8601DateFormatter
,假设 UTC/GMT,如果你愿意:
let formatter = ISO8601DateFormatter()
formatter.formatOptions = [.withFullDate, .withTime, .withColonSeparatorInTime]
而且,在那种情况下,如果您想使用本地时区,则必须指定它:
formatter.timeZone = .current
很遗憾,dateDecodingStrategy
不能使用 ISO8601DateFormatter
。同样,.iso8601
策略假设您将拥有时区字符。
因此,对于 Codable
和您的特定字符串,您必须坚持上面的 DateFormatter
示例。