Swiftpack.co - cyrilchandelier/ISO8601DurationFormatter as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
ISO8601 duration formatter for Swift
.package(url: "https://github.com/cyrilchandelier/ISO8601DurationFormatter.git", from: "0.0.2")


A Swift formatter to support the duration part of the ISO8601 standard.

A formatter similar using an API similar to Swift DateFormatter to convert ISO8601 Duration strings into their TimeInterval equivalent in seconds, and back.


After installing, the package is available using the following import:

import ISO8601DurationFormatter

Swift Package Manager

To integrate ISO8601DurationFormatter into your Xcode project using SPM, simply add the following line to your Package.swift:

.package(url: "https://github.com/cyrilchandelier/ISO8601DurationFormatter", .upToNextMajor(from: "0.0.2"))


To integrate ISO8601DurationFormatter into your Xcode project using CocoaPods, simply add the following line to your Podfile:

pod 'ISO8601DurationFormatter', '~> 0.0.2'


This formatter allows for convertion between ISO8601 Duration tokens and TimeInterval:

Converting String to TimeInterval

The duration string in input must be a valid ISO8601 Duration, or an exception will be thrown.


do {
    let formatter = ISO8601DurationFormatter()
    let decoded = try formatter.duration(from: "PT1M")
    print(decoded) // 60.0
} catch {
    print(String(describing: error))

Note that decimal values are supported, eg: P1.5D or PT1.123S.

Converting TimeInterval to String


do {
    let formatter = ISO8601DurationFormatter()
    let encoded = try formatter.string(from: 120)
    print(encoded) // PT2M
} catch {
    print(String(describing: error))


  • only positive durations are supported: negative durations will trigger an exception
  • empty durations are represented with the following string: P0D


Stars: 0
Last commit: 2 weeks ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Submit a free job ad (while I'm testing this). The analytics numbers for this website are here.

Release Notes

2 weeks ago

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics