Structure
Date
iOS 8.0+
iPadOS 8.0+
Mac Catalyst 8.0+
macOS 10.10+
tvOS 9.0+
watchOS 2.0+
A specific point in time, independent of any calendar or time zone.
struct Date
Overview
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.
Topics
Structures
struct AnchoredRelativeFormatStyleA relative format style that is detached from the system time, and instead formats an anchor date relative to the format input.struct AttributedStyleA structure that creates a locale-appropriate attributed string representation of a date instance.struct ComponentsFormatStylestruct FormatStringA type that represents a fixed date format string using string interpolation.struct FormatStyleA structure that creates a locale-appropriate string representation of a date instance and converts strings of dates and times into date instances.struct HTTPFormatStyleOptions for generating and parsing string representations of dates following the HTTP date format from RFC 9110 § 5.6.7.struct ISO8601FormatStyleA type that converts between dates and their ISO-8601 string representations.struct IntervalFormatStyleA format style that creates string representations of date intervals.struct ParseStrategyOptions for parsing string representations of dates to create aDateinstance.struct RelativeFormatStyleA format style that forms locale-aware string representations of a relative date or time.struct VerbatimFormatStyleA style that formats a date with an explicitly-specified style.
Operators
static func + (Date, TimeInterval) -> DateReturns 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) -> DateReturns aDatewith a specified amount of time subtracted from it.static func -= (inout Date, TimeInterval)Subtract aTimeIntervalfrom aDate.static func == (Date, Date) -> BoolReturns true if the twoDatevalues represent the same point in time.static func < (Date, Date) -> BoolReturns true if the left handDateis earlier in time than the right handDate.static func > (Date, Date) -> BoolReturns true if the left handDateis later in time than the right handDate.
Initializers
init()Creates a date value initialized to the current date and time.init(_:strategy:)init(timeInterval: TimeInterval, since: Date)Creates a date value initialized relative to another given date 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.init(timeIntervalSinceNow: TimeInterval)Creates a date value initialized relative to the current date and time 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.
Instance Properties
var timeIntervalSince1970: TimeIntervalThe interval between the date value and 00:00:00 UTC on 1 January 1970.var timeIntervalSinceNow: TimeIntervalThe time interval between the date value and the current date and time.var timeIntervalSinceReferenceDate: TimeIntervalThe interval between the date value and 00:00:00 UTC on 1 January 2001.
Instance Methods
func ISO8601Format(Date.ISO8601FormatStyle) -> StringGenerates a locale-aware string representation of a date using the ISO 8601 date format.func addTimeInterval(TimeInterval)Adds a time interval to this date.func addingTimeInterval(TimeInterval) -> DateCreates a new date value by adding a time interval to this date.func compare(Date) -> ComparisonResultCompares another date to this one.func description(with: Locale?) -> StringReturns a string representation of the receiver using the given locale.func formatted() -> StringGenerates a locale-aware string representation of a date using the default date format style.func formatted<F>(F) -> F.FormatOutputfunc formatted(date: Date.FormatStyle.DateStyle, time: Date.FormatStyle.TimeStyle) -> StringGenerates a locale-aware string representation of a date using specified date and time format styles.func hash(into: inout Hasher)func timeIntervalSince(Date) -> TimeIntervalReturns the interval between this date and another given date.
Type Properties
static let distantFuture: DateA date value representing a date in the distant future.static let distantPast: DateA date value representing a date in the distant past.static var now: DateReturns a date instance that represents the current date and time, at the moment of access.static let timeIntervalBetween1970AndReferenceDate: TimeIntervalThe number of seconds from 1 January 1970 to the reference date, 1 January 2001.static var timeIntervalSinceReferenceDate: TimeIntervalThe interval between 00:00:00 UTC on 1 January 2001 and the current date and time.
Default Implementations
Relationships
Conforms To
Configuration.ExpressibleByConfigStringSwift.ComparableSwift.CopyableSwift.CustomDebugStringConvertibleSwift.CustomPlaygroundDisplayConvertibleSwift.CustomReflectableSwift.CustomStringConvertibleSwift.DecodableSwift.EncodableSwift.EquatableSwift.EscapableSwift.HashableSwift.SendableSwift.SendableMetatypeSwift.Strideable