Structure

DateComponents

A date or time specified in terms of units (such as year, month, day, hour, and minute) to be evaluated in a calendar system and time zone.

Overview

DateComponents encapsulates the components of a date in an extendable, structured manner.

It is used to specify a date by providing the temporal components that make up a date and time in a particular calendar: hour, minutes, seconds, day, month, year, and so on. It can also be used to specify a duration of time, for example, 5 hours and 16 minutes. A DateComponents is not required to define all the component fields.

When a new instance of DateComponents is created, the date components are set to nil.

Topics

Validating a Date

var isValidDate: Bool

Indicates whether the current combination of properties represents a date which exists in the current calendar.

func isValidDate(in: Calendar)

Indicates whether the current combination of properties represents a date which exists in the specified calendar.

var date: Date?

The date calculated from the current components using the stored calendar.

Accessing Months and Years

var era: Int?

An era or count of eras.

var year: Int?

A year or count of years.

var yearForWeekOfYear: Int?

The ISO 8601 week-numbering year of the receiver.

var quarter: Int?

A quarter or count of quarters.

var month: Int?

A month or count of months.

var isLeapMonth: Bool?

Set to true if these components represent a leap month.

Accessing Weeks and Days

var weekOfMonth: Int?

A week of the month or a count of weeks of the month.

var weekOfYear: Int?

A week of the year or count of the weeks of the year.

var weekday: Int?

A weekday or count of weekdays.

var weekdayOrdinal: Int?

A weekday ordinal or count of weekday ordinals.

var day: Int?

A day or count of days.

Accessing Hours and Seconds

var hour: Int?

An hour or count of hours.

var minute: Int?

A minute or count of minutes.

var second: Int?

A second or count of seconds.

var nanosecond: Int?

A nanosecond or count of nanoseconds.

Accessing Calendar Components

func value(for: Calendar.Component)

Returns the value of one of the properties, using an enumeration value instead of a property name.

func setValue(Int?, for: Calendar.Component)

Set the value of one of the properties, using an enumeration value instead of a property name.

enum Calendar.Component

An enumeration for the various components of a calendar date.

Comparing Date Components

Describing Date Components

var description: String

A textual description of the date components.

var debugDescription: String

A textual description of the date components suitable for debugging.

var customMirror: Mirror

A mirror that reflects the date.

var hashValue: Int

The computed hash value of the date.

Using Reference Types

class NSDateComponents

An object that specifies a date or time in terms of calendar units that bridges to DateComponents; use NSDateComponents when you need reference semantics or other Foundation-specific behavior.

typealias DateComponents.ReferenceType

An alias for this value type's equivalent reference type.

Instance Methods

See Also

Calendrical Calculations

struct Calendar

A definition of the relationships between calendar units (such as eras, years, and weekdays) and absolute points in time, providing features for calculation and comparison of dates.

struct TimeZone

Information about standard time conventions associated with a specific geopolitical region.