A specific point in time, independent of any calendar or time zone.
A `Date` value encapsulates 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.
Creates a date value initialized relative to 00:00:00 UTC on 1 January 2001 by a given number of seconds.
get · iOS 8.0+
timeIntervalSinceReferenceDate
TypeScript
get timeIntervalSinceReferenceDate(): number
Swift declaration
var timeIntervalSinceReferenceDate: TimeInterval { get }
The interval between the date value and 00:00:00 UTC on 1 January 2001.
This property's value is negative if the date object is earlier than the system's absolute reference date (00:00:00 UTC on 1 January 2001).
get · iOS 8.0+
timeIntervalSinceReferenceDate
TypeScript
static get timeIntervalSinceReferenceDate12180(): number
Swift declaration
static var timeIntervalSinceReferenceDate: TimeInterval { get }
The interval between 00:00:00 UTC on 1 January 2001 and the current date and time.
get · iOS 8.0+
timeIntervalBetween1970AndReferenceDate
TypeScript
static get timeIntervalBetween1970AndReferenceDate(): number
Swift declaration
static let timeIntervalBetween1970AndReferenceDate: Double
The number of seconds from 1 January 1970 to the reference date, 1 January 2001.
method · iOS 8.0+
description
TypeScript
descriptionWith(locale?: (Locale) | null): string
Swift declaration
func description(with locale: Locale?) -> String
Returns a string representation of the receiver using the given
locale.
- Parameter locale: A `Locale`. If you pass `nil`, `Date` formats the date in the same way as the `description` property.
- Returns: A string representation of the `Date`, using the given locale, or if the locale argument is `nil`, in the international format `YYYY-MM-DD HH:MM:SS ±HHMM`, where `±HHMM` represents the time zone offset in hours and minutes from UTC (for example, "`2001-03-24 10:45:32 +0600`").
get · iOS 8.0+
description
TypeScript
get description(): string
Swift declaration
var description: String { get }
The representation is useful for debugging only.
There are a number of options to acquire a formatted string for a date including: date formatters
(see [NSDateFormatter](//apple_ref/occ/cl/NSDateFormatter) and
[Data Formatting Guide](//apple_ref/doc/uid/10000029i)), and the `Date`
function `description(locale:)`.
get · iOS 8.0+
distantPast
TypeScript
static get distantPast(): globalThis.Date
Swift declaration
static let distantPast: Date
A date value representing a date in the distant past.
The distant past is in terms of centuries.
get · iOS 8.0+
customMirror
TypeScript
get customMirror(): SDKHandle
Swift declaration
var customMirror: Mirror { get }
The custom mirror for this instance.
If this type has value semantics, the mirror should be unaffected by
subsequent mutations of the instance.
init(timeInterval: TimeInterval, since date: Date)
Creates a date value initialized relative to another given date by a given number of seconds.
- Parameters:
- timeInterval: The number of seconds to add to `date`. A negative value means the receiver will be earlier than `date`.
- date: The reference date.
Generates a locale-aware string representation of a date using the ISO 8601 date format.
Calling this method is equivalent to passing a ``Date/ISO8601FormatStyle`` to a date's ``Date/formatted()`` method.
- Parameter style: A customized ``Date/ISO8601FormatStyle`` to apply. By default, the method applies an unmodified ISO 8601 format style.
- Returns: A string, formatted according to the specified style.
get · iOS 8.0+
distantFuture
TypeScript
static get distantFuture(): globalThis.Date
Swift declaration
static let distantFuture: Date
A date value representing a date in the distant future.
The distant future is in terms of centuries.
Adds a time interval to this date.
- Parameter timeInterval: The value to add, in seconds.
> Warning:
> This only adjusts an absolute value. If you wish to add calendrical concepts like hours, days, months then you must use a `Calendar`. That will take into account complexities like daylight saving time, months with different numbers of days, and more.
get · iOS 8.0+
debugDescription
TypeScript
get debugDescription(): string
Swift declaration
var debugDescription: String { get }
A textual representation of this instance, suitable for debugging.
Calling this property directly is discouraged. Instead, convert an
instance of any type to a string by using the `String(reflecting:)`
initializer. This initializer works with any type, and uses the custom
`debugDescription` property for types that conform to
`CustomDebugStringConvertible`:
struct Point: CustomDebugStringConvertible {
let x: Int, y: Int
var debugDescription: String {
return "(\(x), \(y))"
}
}
let p = Point(x: 21, y: 30)
let s = String(reflecting: p)
print(s)
// Prints "(21, 30)"
The conversion of `p` to a string in the assignment to `s` uses the
`Point` type's `debugDescription` property.
Returns the interval between this date and another given date.
- Parameter date: The date with which to compare this one.
- Returns: The interval between the receiver and the `another` parameter. If the receiver is earlier than `anotherDate`, the return value is negative. If `anotherDate` is `nil`, the results are undefined.
- SeeAlso: `timeIntervalSince1970`
- SeeAlso: `timeIntervalSinceNow`
- SeeAlso: `timeIntervalSinceReferenceDate`
func addingTimeInterval(_ timeInterval: TimeInterval) -> Date
Creates a new date value by adding a time interval to this date.
- Parameter timeInterval: The value to add, in seconds.
- Returns: A new date value calculated by adding a time interval to this date.
> Warning:
> This only adjusts an absolute value. If you wish to add calendrical concepts like hours, days, months then you must use a `Calendar`. That will take into account complexities like daylight saving time, months with different numbers of days, and more.
Creates a date value initialized relative to the current date and time by a given number of seconds.
get · iOS 8.0+
timeIntervalSinceNow
TypeScript
get timeIntervalSinceNow(): number
Swift declaration
var timeIntervalSinceNow: TimeInterval { get }
The time interval between the date value and the current date and time.
If the date is earlier than the current date and time, this property's value is negative.
- SeeAlso: `timeIntervalSince(_:)`
- SeeAlso: `timeIntervalSince1970`
- SeeAlso: `timeIntervalSinceReferenceDate`
Creates a date value initialized relative to 00:00:00 UTC on 1 January 1970 by a given number of seconds.
get · iOS 8.0+
timeIntervalSince1970
TypeScript
get timeIntervalSince1970(): number
Swift declaration
var timeIntervalSince1970: TimeInterval { get }
The interval between the date value and 00:00:00 UTC on 1 January 1970.
This property's value is negative if the date object is earlier than 00:00:00 UTC on 1 January 1970.
- SeeAlso: `timeIntervalSince(_:)`
- SeeAlso: `timeIntervalSinceNow`
- SeeAlso: `timeIntervalSinceReferenceDate`
get · iOS 8.0+
customPlaygroundQuickLook
TypeScript
get customPlaygroundQuickLook(): SDKHandle
Swift declaration
var customPlaygroundQuickLook: PlaygroundQuickLook { get }
A custom playground Quick Look for this instance.
If this type has value semantics, the `PlaygroundQuickLook` instance
should be unaffected by subsequent mutations.
get · iOS 8.0+
now
TypeScript
static get now(): globalThis.Date
Swift declaration
@backDeployed(before: macOS 12, iOS 15, tvOS 15, watchOS 8)
static var now: Date { get }
Returns a date instance that represents the current date and time, at the moment of access.
This property is equivalent to calling ``Date/init()``. If you assign this value to a variable or property, the assigned value doesn't automatically update as time passes.
method · iOS 8.0+
compare
TypeScript
compare(other: globalThis.Date): ComparisonResult
Swift declaration
func compare(_ other: Date) -> ComparisonResult
Compares another date to this one.
- Parameter other: Another date.
method · iOS 13.0+
advanced
TypeScript
advanced(n: number): globalThis.Date
Swift declaration
func advanced(by n: TimeInterval) -> Date
Returns a date offset the specified time interval from this date.
- Parameter n: The time interval offset.
- Returns: A date offset the specified time interval from this date.
method · iOS 13.0+
distance
TypeScript
distance(other: globalThis.Date): number
Swift declaration
func distance(to other: Date) -> TimeInterval
Returns the distance from this date to another date, specified as a time interval.
- Parameter other: Another date.
- Returns: The distance from this date to the other date, as a ``TimeInterval``.
Generates a locale-aware string representation of a date using specified date and time format styles.
When displaying a date to a user, use the convenient `formatted(date:time:)` instance method to customize the string representation of the date. Set the date and time styles of the date format style separately, according to your particular needs.
For example, to create a string with a full date and no time representation, set the date style to `complete` and the time style to `omitted`. Conversely, to create a string representing only the time, set the date style to `omitted` and the time style to `complete`.
```swift
let birthday = Date()
birthday.formatted(date: .complete, time: .omitted) // Sunday, January 17, 2021
birthday.formatted(date: .omitted, time: .complete) // 4:03:12 PM CST
```
You can create string representations of a `Date` instance with several levels of brevity using a variety of preset date and time styles. This example shows date styles of `long`, `abbreviated`, and `numeric`, and time styles of `shortened`, `standard`, and `complete`.
```swift
let birthday = Date()
birthday.formatted(date: .long, time: .shortened) // January 17, 2021, 4:03 PM
birthday.formatted(date: .abbreviated, time: .standard) // Jan 17, 2021, 4:03:12 PM
birthday.formatted(date: .numeric, time: .complete) // 1/17/2021, 4:03:12 PM CST
birthday.formatted() // Jan 17, 2021, 4:03 PM
```
The default date style is `abbreviated` and the default time style is `shortened`.
For the default date formatting, use the `formatted()` method. To customize the formatted measurement string, use the `formatted(_:)` method and include a `Date.FormatStyle`.
For more information about formatting dates, see ``Date/FormatStyle``.
- Parameters:
- date: The style for describing the date part.
- time: The style for describing the time part.
- Returns: A string, formatted according to the specified date and time styles.
method · iOS 15.0+
formatted
TypeScript
formatted(): string
Swift declaration
func formatted() -> String
Generates a locale-aware string representation of a date using the default date format style.
Use the `formatted()` method to apply the default format style to a date, as in the following example:
```swift
let birthday = Date()
print(birthday.formatted())
// 6/4/2021, 2:24 PM
```
The default date format style uses the `numeric` date style and the `shortened` time style.
To customize the formatted measurement string, use either the ``Date/formatted(_:)`` method and include a `Measurement.FormatStyle` or the ``Date/formatted(date:time:)`` and include a date and time style.
For more information about formatting dates, see ``Date/FormatStyle``.
- Returns: A string, formatted according to the default style.
init · iOS 8.0+
init
TypeScript
static init(): globalThis.Date
Swift declaration
init()
Creates a date value initialized to the current date and time.
Skipped declarations
3 declarations were excluded from these bindings. Each reason comes from the generator or the iOS SDK compiler.
Date.formatted(_:)
This declaration needs Swift generic type arguments. (no canonical witness for F)
Date.init(_:strategy:)
This declaration needs Swift generic type arguments. (no canonical witness for T.ParseInput)
Date.init(_:strategy:)
This declaration needs Swift generic type arguments. (no canonical witness for T)