RelativeDateFormat is a small Swift package for formatting dates in a compact, social-app style.
It is useful for timelines, feeds, message lists, activity logs, and any UI where recent dates should be short and easy to scan.
Keywords: Swift, Swift Package Manager, SwiftUI, Foundation, Date, Date FormatStyle, relative date formatter, localized date formatting, timeline date format, social feed timestamp.
Date.now.formatted(.relative(unitsStyle: .condensed))
// "just now"
Date(timeIntervalSinceNow: -60).formatted(.relative(unitsStyle: .condensed))
// "1m"
Date(timeIntervalSinceNow: -86400).formatted(.relative(unitsStyle: .condensed))
// "1d"- iOS 17.0+
- macOS 15.0+
- Swift 6.3+
Add this package to your Swift Package Manager dependencies:
.package(url: "https://github.com/noppefoxwolf/RelativeDateFormat.git", from: "0.0.9")Then add RelativeDateFormat to your target dependencies:
.product(name: "RelativeDateFormat", package: "RelativeDateFormat")Use .relative(unitsStyle: .condensed) with Date.formatted(_:).
import RelativeDateFormat
let text = date.formatted(.relative(unitsStyle: .condensed))You can also pass a fixed reference date, which is useful for tests or stable rendering.
let now = Date(timeIntervalSinceReferenceDate: 0)
let date = Date(timeIntervalSinceReferenceDate: -3600)
date.formatted(.relative(unitsStyle: .condensed, relativeTo: now))
// "1h"The condensed style formats dates using these ranges:
| Range | Example |
|---|---|
| Less than 5 seconds | just now |
| Less than 1 minute | 59s |
| Less than 1 hour | 59m |
| Less than 1 day | 23h |
| Less than 30 days | 29d |
| 30 days or more | Locale-aware short date |
For dates 30 days or older, the package uses Foundation Date.FormatStyle with the current locale.
// en
"04/09/2024"
// ja
"2024/04/09"
// de
"09.04.2024"If the active calendar is Japanese, the date portion is formatted with the Gregorian calendar to avoid showing Japanese era years in compact timeline dates.
Use the SwiftUI Text convenience initializer for compact relative dates.
import RelativeDateFormat
import SwiftUI
Text(date, style: .condensedRelative())You can pass a reference date here as well.
Text(date, style: .condensedRelative(to: referenceDate))If you need direct formatter access, use CondensedStyleRelativeDateTimeFormatter.
import RelativeDateFormat
let formatter = CondensedStyleRelativeDateTimeFormatter()
let text = formatter.localizedString(for: date, relativeTo: Date())Relative strings are localized for:
- English
- German
- Spanish
- French
- Italian
- Japanese
- Korean
- Portuguese (Brazil)
- Simplified Chinese
- Traditional Chinese
RelativeDateFormat is available under the MIT license. See LICENSE for details.