Skip to content

Structure

Date.FormatStyle.Symbol.Hour

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 hour in a date format style.
struct Hour

Overview

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

Factory variable

Description

defaultDigitsNoAMPM

The minimum number of digits that represents the full numeric hour. This doesn’t include the day period (a.m. or p.m.). For example, 1, 11.

twoDigitsNoAMPM

Two-digit numeric hour, zero-padded if necessary. This doesn’t include the day period (a.m. or p.m.). For example, 01, 11.

defaultDigits(amPM:)

The minimum number of digits that represents the full numeric hour. This may include the day period (a.m. or p.m.), depending on locale. For example, 7a (narrow), 7AM (abbreviated), 7A.M. (wide).

twoDigits(amPM:)

Two-digit numeric hour, zero-padded if necessary. This may include the day period (a.m. or p.m.), depending on locale. For example, 07a (narrow), 07AM (abbreviated), 07A.M. (wide).

conversationalDefaultDigits(amPM:)

The minimum number of digits that represents the full numeric hour. This may include the day period (a.m. or p.m.), depending on locale, and can include conversational period formats. For example, 7a (narrow), 7AM (abbreviated), 7A.M. (wide).

conversationalTwoDigits(amPM:)

Two-digit numeric hour, zero-padded if necessary. This may include the day period (a.m. or p.m.), depending on locale, and can include conversational period formats. For example, 07a (narrow), 07AM (abbreviated), 07A.M. (wide).

To customize the hour format in a string representation of a Date, use hour(_:) The example below shows a variety of Date.FormatStyle.Symbol.Hour format styles applied to a date.

let meetingDate = Date() // Feb 9, 2021 at 7:00 PM
meetingDate.formatted(Date.FormatStyle().hour(.defaultDigitsNoAMPM))
// 7

meetingDate.formatted(Date.FormatStyle().hour(.twoDigitsNoAMPM))
// 07

meetingDate.formatted(Date.FormatStyle().hour(.defaultDigits(amPM: .narrow)))
// 7p

meetingDate.formatted(Date.FormatStyle().hour(.twoDigits(amPM: .abbreviated))
// 07 PM

meetingDate.formatted(Date.FormatStyle().hour(.conversationalDefaultDigits(amPM: .wide))
// 7 P.M.

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

Structures

  • struct AMPMStyle
    The format style of the string representation of the day period, before or after noon, in a date.

Type Properties

Type Methods

Relationships

Conforms To

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