Structure
Date.FormatStyle.Symbol.Year
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 |
|---|---|
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())) // 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
Type Properties
static var defaultDigits: Date.FormatStyle.Symbol.YearMinimum number of digits that shows the full year. For example,2,20,201,2017,20173.static let omitted: Date.FormatStyle.Symbol.YearThe option for not including the symbol in the formatted output.static var twoDigits: Date.FormatStyle.Symbol.YearTwo low-order digits. Padded or truncated if necessary. For example,02,20,01,17,73.
Type Methods
static func extended(minimumLength: Int) -> Date.FormatStyle.Symbol.YearExtended 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.static func padded(Int) -> Date.FormatStyle.Symbol.YearThree or more digits. Padded if necessary. For example,002,020,201,2017,20173.static func relatedGregorian(minimumLength: Int) -> Date.FormatStyle.Symbol.YearRelated 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壬辰年腊月初四”.
Relationships
Conforms To
Swift.EquatableSwift.HashableSwift.SendableSwift.SendableMetatype