dateTime
A style for formatting a date and time.
Declaration
static var dateTime: Date.FormatStyle { get }Discussion
Use this type property when the call point allows the use of Date.FormatStyle. You typically do this when calling the formatted(_:) method of Date.
Customize the date format style using modifier syntax to apply specific date and time formats. For example:
let meetingDate = Date()
let localeArray = ["en_US", "sv_SE", "en_GB", "th_TH", "fr_BE"]
let formattedDates = localeArray.map { localeID in
meetingDate.formatted(.dateTime
.day(.twoDigits)
.month(.wide)
.weekday(.short)
.hour(.conversationalTwoDigits(amPM: .wide))
.locale(Locale(identifier: localeID)))
} // ["Mo, July 31 at 05 PM", "må 31 juli 17", "Mo, 31 July at 17", "จ. 31 กรกฎาคม เวลา 17", "lu 31 juillet à 17 h"]The default format styles provided are numeric date format and shortened time format. For example:
let meetingDate = Date()
let formatted = meetingDate.formatted(.dateTime) // "7/31/2023, 5:15 PM"