Swift 5 扩展,将 ISO 8601 持续时间字符串转换为 DateComponents
对象。
PT12H = 12 小时
P3D = 3 天
P3DT12H = 3 天,12 小时
P3Y6M4DT12H30M5S = 3 年,6 个月,4 天,12 小时,30 分钟和 5 秒
P10W = 70 天
欲了解更多信息,请访问:https://en.wikipedia.org/wiki/ISO_8601#Durations
let durationString = "P3Y6M4DT12H30M5S"
let components = DateComponents.durationFrom8601String(durationString)
components?.year // 3
components?.month // 6
components?.day // 4
components?.hour // 12
components?.minute // 30
components?.second // 5
let durationStringInWeeks = "P8W"
let componentsForWeeks = DateComponents.durationFrom8601String(durationStringInWeeks)
componentsForWeeks?.day //56
您可以使用 SPM。将以下内容添加到您的 Package.swift
文件中。
.package(url: "https://github.com/leonx98/Swift-ISO8601-DurationParser.git", .upToNextMajor(from: "1.0.0")),
如果您喜欢我的开源库,可以赞助它!
Swift/ISO8601-Duration Parser 基于 MIT 许可证发布。有关更多信息,请参见 LICENSE 文件。
Objective-C 版本来自 @kevinrandrup