Skip to content

Instance MethodFoundationEssentials

formatted()

iOS 15.0+ iPadOS 15.0+ Mac Catalyst 15.0+ macOS 12.0+ tvOS 15.0+ watchOS 8.0+
Generates a locale-aware string representation of a date using the default date format style.
func formatted() -> String

Return Value

A string, formatted according to the default style.

Discussion

Use the formatted() method to apply the default format style to a date, as in the following example:

let birthday = Date()
print(birthday.formatted())
// 6/4/2021, 2:24 PM

The default date format style uses the numeric date style and the shortened time style.

To customize the formatted measurement string, use either the formatted(_:) method and include a Measurement.FormatStyle or the formatted(date:time:) and include a date and time style.

For more information about formatting dates, see Date.FormatStyle.