ISO8601DurationFormatter
A formatter for converting ISO 8601 durations to DateComponents.
Installation
Swift Package Manager
Add the following to your Package.swift file's dependencies:
.package(url: "https://github.com/kkla320/ISO8601DurationFormatter.git", from: "1.2.0")
And then import wherever needed
import ISO8601DurationFormatter
Example
Converting a string to DateComponents
let input = "PT40M"
let dateComponents = formatter.dateComponents(from: input)
if let dateComponents = dateComponents {
print(dateComponents.minute) // 40
}
Converting DateComponents to string
let dateComponents = DateComponents(year: 6,
month: 2,
day: 2,
hour: 4,
minute: 44,
second: 22,
weekOfYear: 2)
let ISO8601DurationString = dateComponents.toISO8601Duration()
print(ISO8601DurationString) // P6Y2M2W2DT4H44M22S
Special thanks to Igor-Palaguta for implementing the most in his project YoutubeEngine.