Class

NSDateInterval

An object representing the span of time between two dates that bridges to DateInterval; use NSDateInterval when you need reference semantics or other Foundation-specific behavior.

Overview

An NSDateInterval object represents a closed interval between two dates. The NSDateInterval class provides a programmatic interface for calculating the duration of a time interval and determining whether a date falls within it, as well as comparing date intervals and checking to see whether they intersect.

An NSDateInterval object consists of a startDate and an endDate. The startDate and endDate of a date interval can be equal, in which case its duration is 0. However, endDate cannot occur earlier than startDate.

You can use the DateIntervalFormatter class to create string representations of NSDateInterval objects that are suitable for display in the current locale.

Topics

Creating Date Intervals

init()

Initializes a date interval by setting the start and end date to the current date.

init(start: Date, duration: TimeInterval)

Initializes a date interval with a given start date and duration.

init(start: Date, end: Date)

Initializes a date interval from a given start date and end date.

init(coder: NSCoder)

Returns a date interval initialized from data in the given unarchiver.

Accessing Start Date, End Date, and Duration

var startDate: Date

The start date of the date interval.

var endDate: Date

The end date of the date interval.

var duration: TimeInterval

The duration of the date interval.

Comparing Date Intervals

func compare(DateInterval)

Compares the receiver with the specified date interval.

func isEqual(to: DateInterval)

Indicates whether the receiver is equal to the specified date interval.

Determining Intersections

func intersects(DateInterval)

Indicates whether the receiver intersects with the specified date interval.

func intersection(with: DateInterval)

Returns the intersection between the receiver and the specified date interval.

Determining Whether a Date Occurs Within a Date Interval

func contains(Date)

Indicates whether the receiver contains the specified date.

Relationships

Inherits From

See Also

Using Reference Types

typealias DateInterval.ReferenceType

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