Date.FormatStyle.Symbol.Year
A type that specifies a format for the year in a date format style.
Declaration
struct YearOverview
The Date.FormatStyle.Symbol.Year type includes static factory variables and methods that create custom Date.FormatStyle.Symbol.Year objects:
Factory variable | Description |
|---|---|
The minimum number of digits that represents the full year. For example, | |
The year’s two lowest-order digits, zero-padded or truncated if necessary. For example, | |
Three or more digits, zero-padded if necessary. For example, | |
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. | |
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())) // 2021If 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
Date.FormatStyle.Symbol.CyclicYearDate.FormatStyle.Symbol.DayDate.FormatStyle.Symbol.DayOfYearDate.FormatStyle.Symbol.DayPeriodDate.FormatStyle.Symbol.EraDate.FormatStyle.Symbol.HourDate.FormatStyle.Symbol.MinuteDate.FormatStyle.Symbol.MonthDate.FormatStyle.Symbol.QuarterDate.FormatStyle.Symbol.SecondDate.FormatStyle.Symbol.SecondFractionDate.FormatStyle.Symbol.StandaloneMonthDate.FormatStyle.Symbol.StandaloneQuarterDate.FormatStyle.Symbol.StandaloneWeekdayDate.FormatStyle.Symbol.TimeZone