Date & FormatStyle & You
This is part of the FormatStyle Deep Dive series
Apple provides seven different FormatStyle
variants that covers an impressive array of use cases for date formatting.
At it’s most basic, calling the .formatted()
on any Date
object will show you a basic representation:
Date(timeIntervalSinceReferenceDate: 0).formatted() // "12/31/2000, 5:00 PM"
Sidenote: The default is identical to calling
.formatted(date: .numeric, time: .shortened)
Download the Xcode Playground with all examples
Date.FormatStyle
Allows us to customize the DateStyle, TimeStyle, Locale and Calendar to display the date in fixed ways. Apple provides a shortcut to this struct when you use the .formatted(date: time:)
method on the Date object..
twosday.formatted(date: .complete, time: .complete) // "Tuesday, February 22, 2022, 2:22:22 AM MST"
Date.FormatStyle.dateTime()
Allows you to specify the exact date components you would like to output. Furthermore, these components can be individually configured for maximum flexibility.
twosday.formatted(.dateTime.year().month().day().hour().minute().second()) // "Feb 22, 2022, 2:22:22 AM"
Date.ISO8601FormatStyle
Allows you to create strings that conform to the ISO 8601 date standard by calling .formatted(.iso8601)
on any date object. You can further customize a few options on the output, as well as the ability to set the locale, calendar, and time zone.
isoFormat.format(twosday) // "2022-02-22T09:22:22.000Z"
Date.ComponentsFormatStyle
Allows you to show the number of years, months, weeks, days, hours, minutes, and seconds that have passed between a range of Date objects. It’s customizable as to what units you show, and can further be customized with the locale and calendar.
// "21 yrs, 1 mth, 3 wks, 9 hr, 1,342 sec"
secondRange.formatted(.components(style: .abbreviated, fields: [.day, .month, .year, .hour, .second, .week]))
Date.IntervalFormatStyle
Given a range of Date objects, you can display simply the earliest and latest dates as a string by calling the .formatted(.interval)
method on a Date object. You can further customize the locale, calendar, and time zone of the display.
range.formatted(.interval) // "12/31/69, 5:00 PM – 12/31/00, 5:47 PM"
Date.RelativeFormatStyle
When called on a date, it outputs a plain language string of the distance between that date and now e.g. “2 weeks ago” by calling the .formatted(.relative(presentation: unitsStyle:))
extension on FormatStyle
.
thePast.formatted(.relative(presentation: .numeric)) // "2 weeks ago"
Date.VerbatimFormatStyle
The only FormatStyle that isn’t localized. The Date.VerbatimFormatStyle
lets you specify the individual components you’d like to display, as well as the calendar and time zone.
Download the Xcode Playground with all examples