Skip to content

Instance MethodFoundationInternationalization

format(_:)

iOS 15.0+ iPadOS 15.0+ Mac Catalyst 15.0+ macOS 12.0+ tvOS 15.0+ watchOS 8.0+
Creates a locale-aware string representation from a date value.
func format(_ value: Date) -> String

Parameters

value

The date to format.

Return Value

A string representation of the date.

Discussion

Once you create a style, you can use it to format dates multiple times.

The following example creates a format style to guide parsing a set of string representations of dates. It also creates a second format style, applying it repeatedly to produce more detailed string representations of those dates for a different locale.

let inputFormat = Date.FormatStyle()
    .locale(Locale(identifier: "en_GB"))
    .year()
    .month()
    .day()

let iphoneIntroductionDate = try! Date("9 Jan 2007", strategy: inputFormat)
let ipadIntroductionDate = try! Date("27 Jan 2010", strategy: inputFormat)
let wwdc2021Date = try! Date("7 Jun 2021", strategy: inputFormat)

let outputFormat = Date.FormatStyle()
    .locale(Locale(identifier: "en_US"))
    .year()
    .month(.wide)
    .day(.twoDigits)
    .weekday(.abbreviated)

print(outputFormat.format(wwdc2021Date))
// Mon, June 07, 2021

print(outputFormat.format(ipadIntroductionDate))
// Wed, January 27, 2010

print(outputFormat.format(iphoneIntroductionDate))
// Tue, January 09, 2007