Instance Method

dateComponents(_:from:to:)

Returns the difference between two dates specified as DateComponents.

Declaration

func dateComponents(_ components: Set<Calendar.Component>, from start: DateComponents, to end: DateComponents) -> DateComponents

Parameters

components

Which components to compare.

start

The starting date components.

end

The ending date components.

Return Value

The result of calculating the difference from start to end.

Discussion

For components which are not specified in each DateComponents, but required to specify an absolute date, the base value of the component is assumed. For example, for an DateComponents with just a year and a month specified, a day of 1, and an hour, minute, second, and nanosecond of 0 are assumed. Calendrical calculations with unspecified year or year value prior to the start of a calendar are not advised. For each DateComponents, if its timeZone property is set, that time zone is used for it. If the calendar property is set, that is used rather than the receiving calendar, and if both the calendar and timeZone are set, the timeZone property value overrides the time zone of the calendar property.

See Also

Extracting Components

func date(Date, matchesComponents: DateComponents)

Determines if the date has all of the specified date components.

func component(Calendar.Component, from: Date)

Returns the value for one component of a date.

func dateComponents(Set<Calendar.Component>, from: Date)

Returns all the date components of a date, using the calendar time zone.

func dateComponents(in: TimeZone, from: Date)

Returns all the date components of a date, as if in a given time zone (instead of the Calendar time zone).

enum Calendar.Component

An enumeration for the various components of a calendar date.