TimeLabels
Swift packageTimeLabels is a Swift package of short, deterministic date and time labels for UIs: "In 2h", "5mins ago", "3:30 PM", "Tomorrow". It saves you from repeating DateFormatter setup on every screen.
What it does
- Time until and since.
timeRemaining,timeAgoandrelative, which picks "In 5mins" or "5mins ago" for you. - Durations.
durationgives a compact ("1h 30m") or countdown ("1:30:00") label, from seconds or a SwiftDuration. - Clock and dates.
time,timeRange,date("Tomorrow") anddateTime("Tomorrow at 3:30 PM"). - Units and rounding. Seconds, days and weeks, with optional rounding to the nearest unit. Options include
expandUnits,addSpace,spellNumbers,use24HourandlargestUnit. - Date extensions and SwiftUI. Shortcuts like
date.timeAgoLabel()and a self-updatingTimeLabelTextview. - Predictable output. A fixed
en_US_POSIXlocale, no shared mutable state, and anow:parameter so tests can pin the clock.
Example
TimeLabel.timeRemaining(1_800) // "In 30mins"
TimeLabel.timeAgo(date.addingTimeInterval(-300)) // "5mins ago"
TimeLabel.time(Date()) // "3:30 PM"
Install
In Xcode, choose File > Add Package Dependencies, enter https://github.com/founding5067/TimeLabels and use "Up to Next Major Version" from 2.0.0. Or add it to Package.swift:
.package(url: "https://github.com/founding5067/TimeLabels", from: "2.0.0")
Requirements
Swift 6.0+ on iOS 16, macOS 13, watchOS 9, tvOS 16 or visionOS 1. CI also runs on Linux. MIT licensed.