Swift ISO 8601 标准格式日期格式和ISO8601DateFormatter
Swift ISO 8601 标准格式日期格式和ISO8601DateFormatter

Swift ISO 8601 标准格式日期格式和ISO8601DateFormatter

ISO 8601 是国际标准化组织(ISO)制定的 日期和时间的国际标准格式,全称是:

ISO 8601: Data elements and interchange formats – Information interchange – Representation of dates and times

这个标准主要用于确保日期和时间的表达在全球范围内是一致的,避免地区间格式差异造成的歧义。

格式规范

日期(Date):YYYY-MM-DD,如:

2025-05-04

时间(Time):hh:mm:ss,如:

11:39:23

日期 + 时间(DateTime):YYYY-MM-DDThh:mm:ss

2025-05-04T11:39:23

注意中间的 T 是固定分隔符,表示“时间的开始”。

加上时区(Z 代表 UTC):

2025-05-04T14:23:45Z           // UTC 时间
2025-05-04T14:23:45+08:00      // UTC+8,北京时间

含毫秒

2025-05-04T14:23:45.123Z

为什么使用 ISO 8601?

1、消除歧义(比如美式 MM/DD/YYYY 与欧式 DD/MM/YYYY 容易混淆)

2、易于排序(年月日从大到小,有利于字符串比较)

3、跨系统兼容性强,特别适用于 API 和数据库存储

4、由机器容易解析,可以直接转为 Date 类型

ISO8601DateFormatter

ISO8601DateFormatter 是一个专门用于解析和生成符合 ISO 8601 标准格式(例如 “2025-05-04T14:00:00Z”)日期字符串的格式化器。在 SwiftUI 或 Swift 中,你可以直接使用它来格式化 Date 对象。

创建格式化器

let isoFormatter = ISO8601DateFormatter()

将 Date 转换为字符串

let now = Date()
let isoString = isoFormatter.string(from: now)
print(isoString)  // e.g., "2025-05-04T12:34:56Z"

将 ISO 字符串转换为 Date

let dateString = "2025-05-04T12:34:56Z"
if let date = isoFormatter.date(from: dateString) {
    print(date)
}

使用选项(如含毫秒)

let formatter = ISO8601DateFormatter()
formatter.formatOptions = [.withInternetDateTime, .withFractionalSeconds]

let now = Date()
let string = formatter.string(from: now)  // e.g., "2025-05-04T12:34:56.789Z"

SwiftUI中结合使用

在 SwiftUI 中可以使用它与 Text 视图结合,例如:

struct ContentView: View {
    let date = Date()
    let formatter: ISO8601DateFormatter = {
        let f = ISO8601DateFormatter()
        f.formatOptions = [.withInternetDateTime, .withFractionalSeconds]
        return f
    }()
    
    var body: some View {
        Text(formatter.string(from: date))
    }
}

输出的文本内容为:

2025-05-04T12:01:53.256Z

FormatOptions

ISO8601DateFormatter.FormatOptions 是一个 OptionSet,它通过组合不同选项,控制 ISO8601DateFormatter 如何解析或格式化时间字符串。

1、.withYear:包含年份(如:2025)

2、.withMonth:包含月份(如:05)

3、.withDay:包含日期(如:04)

4、.withTime:包含时间(如:03:25:15)

5、.withTimeZone:包含时区(如:Z 或 +08:00)

6、.withSpaceBetweenDateAndTime:日期与时间之间用空格而非 T

7、.withDashSeparatorInDate:日期用 – 分隔(推荐)

8、.withColonSeparatorInTime:时间用 : 分隔(推荐)

9、.withColonSeparatorInTimeZone:时区用 : 分隔

10、.withFractionalSeconds:包含毫秒(如:.451)

11、.withFullDate:相当于:.withYear + .withMonth + .withDay + .withDashSeparatorInDate

12、.withFullTime:相当于:.withTime + .withTimeZone + .withColonSeparatorInTime + .withColonSeparatorInTimeZone

13、.withInternetDateTime:等价于:.withFullDate + .withTime + .withTimeZone + .withDashSeparatorInDate + .withColonSeparatorInTime + .withColonSeparatorInTimeZone,这是处理大多数 API 最常用的选项

相关文章

Swift使用JSONDecoder解码日期之ISO 8601 标准格式:https://fangjunyu.com/2024/11/11/swift%e4%bd%bf%e7%94%a8-iso-8601-%e6%a0%87%e5%87%86%e6%a0%bc%e5%bc%8f%e6%9d%a5%e8%a7%a3%e6%9e%90%e6%97%a5%e6%9c%9f/

   

如果您认为这篇文章给您带来了帮助,您可以在此通过支付宝或者微信打赏网站开发者。

欢迎加入我们的 微信交流群QQ交流群,交流更多精彩内容!
微信交流群二维码 QQ交流群二维码

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注