A type that specifies a format for the year in a date format style.
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 ``Date/FormatStyle/year(_:)``. The following example shows a variety of ``Date/FormatStyle/Symbol/Year`` formats applied to a date.
```swift
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 ``Date/FormatStyle/Symbol/Year/defaultDigits`` static variable is the default format.
For more information about formatting dates, see the ``Date/FormatStyle``.
Members (6)
get · iOS 15.0+
defaultDigits
TypeScript
static get defaultDigits(): Date_FormatStyle_Symbol_Year
Swift declaration
static var defaultDigits: Date.FormatStyle.Symbol.Year { get }
Minimum number of digits that shows the full year.
For example, `2`, `20`, `201`, `2017`, `20173`.
static func relatedGregorian(minimumLength: Int = 1) -> Date.FormatStyle.Symbol.Year
Related Gregorian year.
For non-Gregorian calendars, this corresponds to the extended Gregorian year in which the calendar’s year begins. Related Gregorian years are often displayed, for example, when formatting dates in the Japanese calendar — e.g. "2012(平成24)年1月15日" — or in the Chinese calendar — e.g. "2012壬辰年腊月初四".
static func extended(minimumLength: Int = 1) -> Date.FormatStyle.Symbol.Year
Extended year.
This is a single number designating the year of this calendar system, encompassing all supra-year fields. For example, for the Julian calendar system, year numbers are positive, with an era of BCE or CE. An extended year value for the Julian calendar system assigns positive values to CE years and negative values to BCE years, with 1 BCE being year 0.
get · iOS 15.0+
twoDigits
TypeScript
static get twoDigits(): Date_FormatStyle_Symbol_Year
Swift declaration
static var twoDigits: Date.FormatStyle.Symbol.Year { get }
Two low-order digits.
Padded or truncated if necessary. For example, `02`, `20`, `01`, `17`, `73`.