Autumn Sale50% offCOMPOT
Texts & TypographyFree ComponentiOS 16.0+Xcode 15+

Relative Date Text

Automatically updating relative time formatting (e.g. "2 hours ago", "in 3 days").

SwiftUI Implementation

Pure Vanilla Swift • No Dependencies
textWDateFormatRelative.swift
import SwiftUI

struct TextWDateFormatRelative: View {
    
    let now: Date = Date.now
    let todayStart: Date = Calendar.current.startOfDay(for: Date.now)
    let yesterday: Date = Calendar.current.date(byAdding: .day, value: -1, to: Date.now)!
    let tomorrow: Date = Calendar.current.date(byAdding: .day, value: 1, to: Date.now)!

    
    var body: some View {
        VStack {
            Text(now, format: .relative(presentation: .named, unitsStyle: .spellOut))
            Text(todayStart, format: .relative(presentation: .named, unitsStyle: .wide))
            Text(yesterday, format: .relative(presentation: .named, unitsStyle: .spellOut))
            Text(tomorrow, format: .relative(presentation: .named, unitsStyle: .spellOut))
        }
    }
}

#Preview {
    TextWDateFormatRelative()
}

Ready to copy and paste directly into any Xcode project running iOS 16.0+.

Recommended Use Cases

  • App settings, preferences, and account configuration screens

Component Details

iOS Compatibility
iOS 16.0+
Xcode Version
Xcode 15+
Platform
iOS
License Tier
Free
Themes & Contexts
settings
C

Compot for iOS

Native SwiftUI playground app

Preview all 130+ components interactively on your device and copy code straight to Xcode with 1 tap.

Get Compot on the App Store