Contents

Date.FormatStyle.Symbol.Year

A type that specifies a format for the year in a date format style.

Declaration

struct Year

Overview

The Date.FormatStyle.Symbol.Year type includes static factory variables and methods that create custom Date.FormatStyle.Symbol.Year objects:

Factory variable

Description

Defaultdigits

The minimum number of digits that represents the full year. For example, 2, 20, 201, 2017.

Twodigits

The year’s two lowest-order digits, zero-padded or truncated if necessary. For example, 02, 20, 01, 17, 73.

Padded(_:)

Three or more digits, zero-padded if necessary. For example, 002, 020, 201, 2017.

Relatedgregorian(minimumlength:)

For non-Gregorian calendars, output corresponds to the extended Gregorian year in which the calendar’s year begins. The default length is the minimum needed to show the full year.

Extended(minimumlength:)

A single number designating the year of the calendar system, encompassing all supra-year fields. The default length is the minimum needed to show the full year.

To customize the year format in a string representation of a Date, use year(_:). The following 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(.extended(minimumLength: 5))) // 02021
meetingDate.formatted(Date.FormatStyle().year(.extended())) // 2021
meetingDate.formatted(Date.FormatStyle().year(.padded(6))) // 002021
meetingDate.formatted(Date.FormatStyle().year(.relatedGregorian())) // 2021

If no format is specified as a parameter, the defaultDigits static variable is the default format.

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

Topics

Modifying a Year

Comparing Years

Type Properties

See Also

Modifying Date Style Format Symbols