Structure

Date

A specific point in time, independent of any calendar or time zone.

Overview

A Date value encapsulate a single point in time, independent of any particular calendrical system or time zone. Date values represent a time interval relative to an absolute reference date.

The Date structure provides methods for comparing dates, calculating the time interval between two dates, and creating a new date from a time interval relative to another date. Use date values in conjunction with DateFormatter instances to create localized representations of dates and times and with Calendar instances to perform calendar arithmetic.

Date bridges to the NSDate class. You can use these interchangeably in code that interacts with Objective-C APIs.

Topics

Creating a Date

init()

Creates a date value initialized to the current date and time.

init(timeIntervalSinceNow: TimeInterval)

Creates a date value initialized relative to the current date and time by a given number of seconds.

init(timeInterval: TimeInterval, since: Date)

Creates a date value initialized relative to another given date by a given number of seconds.

init(timeIntervalSinceReferenceDate: TimeInterval)

Creates a date value initialized relative to 00:00:00 UTC on 1 January 2001 by a given number of seconds.

init(timeIntervalSince1970: TimeInterval)

Creates a date value initialized relative to 00:00:00 UTC on 1 January 1970 by a given number of seconds.

Getting Temporal Boundaries

static let distantFuture: Date

A date value representing a date in the distant future.

static let distantPast: Date

A date value representing a date in the distant past.

Comparing Dates

static func <(Date, Date)

Returns a Boolean that is true if the left hand date is earlier in time than the right hand date.

static func ==(Date, Date)

Returns a Boolean that is true if the two date values represent the same point in time.

static func >(Date, Date)

Returns a Boolean that is true if the left hand date is later in time than the right hand date.

func compare(Date)

Compares two date values.

Getting Time Intervals

func timeIntervalSince(Date)

Returns the interval between the receiver and another given date.

var timeIntervalSinceNow: TimeInterval

The time interval between the date value and the current date and time.

var timeIntervalSinceReferenceDate: TimeInterval

The interval between the date value and 00:00:00 UTC on 1 January 2001.

var timeIntervalSince1970: TimeInterval

The interval between the date value and 00:00:00 UTC on 1 January 1970.

static var timeIntervalSinceReferenceDate: TimeInterval

The interval between 00:00:00 UTC on 1 January 2001 and the current date and time.

static let timeIntervalBetween1970AndReferenceDate: TimeInterval

The number of seconds from 1 January 1970 to the reference date, 1 January 2001.

Adding or Subtracting a Time Interval

static func !=(Date, Date)

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

static func +(Date, TimeInterval)

Returns a date with a specified amount of time added to it.

static func +=(inout Date, TimeInterval)

Adds a time interval to a date.

static func -(Date, TimeInterval)

Returns a date with a specified amount of time subtracted from it.

static func -=(inout Date, TimeInterval)

Subtracts a time interval from a date.

static func <(Date, Date)

Returns a Boolean that is true if the left hand date is earlier in time than the right hand date.

static func <=(Date, Date)

Returns a Boolean value indicating whether the value of the first argument is less than or equal to that of the second argument.

static func ==(Date, Date)

Returns a Boolean that is true if the two date values represent the same point in time.

static func >(Date, Date)

Returns a Boolean that is true if the left hand date is later in time than the right hand date.

static func >(Date, Date)

Returns a Boolean value indicating whether the value of the first argument is greater than that of the second argument.

static func >=(Date, Date)

Returns a Boolean value indicating whether the value of the first argument is greater than or equal to that of the second argument.

Describing Dates

var description: String

A textual description of the date value.

func description(with: Locale?)

Returns a string representation of the date using the given locale.

var debugDescription: String

A textual description of the date suitable for debugging.

var customMirror: Mirror

A mirror that reflects the date.

var hashValue: Int

The computed hash value of the date.

var customPlaygroundQuickLook: PlaygroundQuickLook

A custom playground Quick Look for the date.

Using Reference Types

class NSDate

A representation of a specific point in time that bridges to Date; use NSDate when you need reference semantics or other Foundation-specific behavior.

typealias Date.ReferenceType

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

Instance Methods

func addTimeInterval(TimeInterval)

Adds a time interval to this date.

func addingTimeInterval(TimeInterval)

Creates a new date value by adding a time interval to this date.

Operator Functions

static func ...(Date)

Returns a partial range extending upward from a lower bound.

static func ...(Date)

Returns a partial range up to, and including, its upper bound.

static func ..<(Date)

Returns a partial range up to, but not including, its upper bound.

See Also

Date Representations

struct DateInterval

The span of time between a specific start date and end date.

typealias TimeInterval

A number of seconds.