Contents

year(_:)

Modifies the date format style to use the specified year format style.

Declaration

func year(_ format: Date.FormatStyle.Symbol.Year = .defaultDigits) -> Date.FormatStyle

Parameters

  • format:

    The year format style applied to the date format style.

Return Value

A date format style modified to include the specified year format style.

Discussion

Possible values of Date.FormatStyle.Symbol.Year include twoDigits, padded(_:), relatedGregorian(minimumLength:), and extended(minimumLength:).

This example shows a variety of Date.FormatStyle.Symbol.Year formats applied to a date:

let meetingDate = Date() // Feb 9, 2021 at 3:00 PM
meetingDate.formatted(Date.FormatStyle().year(.defaultDigits)) // 2021
meetingDate.formatted(Date.FormatStyle().year(.twoDigits)) // 21
meetingDate.formatted(Date.FormatStyle().year(.padded(6))) // 002021

If you don’t provide a format, the defaultDigits static variable is the default format.

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

See Also

Specifying the Date Format