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.


struct DateComponents


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.


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) -> Bool

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 year corresponding to a week-counting week.

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) -> Int?

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

static func != (DateComponents, DateComponents) -> Bool

Returns a Boolean value indicating whether two values are not equal.

static func == (DateComponents, DateComponents) -> Bool

Indicates whether two date component instances are the same.

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.

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.

Beta Software

This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.

Learn more about using Apple's beta software