Skip to content

Structure

Date.FormatStyle.Symbol.Year

iOS 15.0+ iPadOS 15.0+ Mac Catalyst 15.0+ macOS 12.0+ tvOS 15.0+ watchOS 8.0+
A type that specifies a format for the year in a date format style.
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

Type Properties

Type Methods

Relationships

Conforms To

  • Swift.Equatable
  • Swift.Hashable
  • Swift.Sendable
  • Swift.SendableMetatype