首页 > 解决方案 > 我可以使用 NSISO8601DateFormatter 解析 ISO-8601 字符串吗?

问题描述

macOS 10.12 中的 Foundation 有一个新类 ,NSISO8601DateFormatter它声称能够解析 ISO-8601,但我无法理解如何使用它。

它需要 a .formatOptions,它说它也用于解析。ISO-8601 的全部意义不在于它是一种明确的格式吗?

例如,这有效:

let f = ISO8601DateFormatter()
f.formatOptions = [.withFullDate]
f.date(from: "2018-11-26")  // a Date

但这忽略了时间:

let f = ISO8601DateFormatter()
f.formatOptions = [.withFullDate]
f.date(from: "2018-11-26T18:07:13Z")  // a Date but wrong

如果我添加.withFullTime,这可行,但第一个示例失败。

如何使用 NSISO8601DateFormatter 解析 ISO-8601?我需要先自行预解析它以找出它使用的 ISO-8601 的子格式吗?

标签: macosmacos-sierrafoundationiso8601

解决方案


推荐阅读