NSDate
A representation of a specific point in time, independent of any calendar or time zone.
Declaration
class NSDateMentioned in
Overview
In Swift, use this type when you need reference semantics or other Foundation-specific behavior.
NSDate objects encapsulate a single point in time, independent of any particular calendrical system or time zone. Date objects are immutable, representing an invariant time interval relative to an absolute reference date (00:00:00 UTC on 1 January 2001).
The NSDate class 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. NSDate objects can be used in conjunction with DateFormatter objects to create localized representations of dates and times, as well as with NSCalendar objects to perform calendar arithmetic.
NSDate is toll-free bridged with its Core Foundation counterpart, CFDate. See Toll-Free Bridging for more information on toll-free bridging.
Subclassing Notes
You might subclass NSDate in order to make it easier to work with a particular calendrical system, or to work with date and time values with a finer temporal granularity.
Methods to Override and Other Requirements
If you want to subclass NSDate to obtain behavior different than that provided by the private or public subclasses, you must:
Declare a suitable instance variable to hold the date and time value (relative to an absolute reference date)
Override the timeIntervalSinceReferenceDate instance method to provide the correct date and time value based on your instance variable
Override init(timeIntervalSinceReferenceDate:), one of the designated initializer methods
If creating a subclass that represents a calendrical system, define methods that partition past and future periods into the units of this calendar
Implement the methods required by the NSCopying and NSCoding protocols, because NSDate adopts these protocols
Special Considerations
Your subclass may use a different reference date than the absolute reference date used by NSDate (00:00:00 UTC on 1 January 2001). If it does, it must still use the absolute reference date in its implementations of the methods timeIntervalSinceReferenceDate and init(timeIntervalSinceReferenceDate:). That is, the reference date referred to in the titles of these methods is the absolute reference date. If you do not use the absolute reference date in these methods, comparisons between NSDate objects of your subclass and NSDate objects of a private subclass will not work.
Topics
Initializing a Date
init()init(timeIntervalSinceNow:)init(timeIntervalSinceReferenceDate:)init(timeIntervalSince1970:)init(coder:)
Getting Temporal Boundaries
Retrieving the Current Date
Comparing Dates
Getting Time Intervals
timeIntervalSince(_:)timeIntervalSinceNowtimeIntervalSinceReferenceDatetimeIntervalSince1970timeIntervalSinceReferenceDateNSTimeIntervalSince1970
Adding Time Intervals
Describing Dates
Recognizing Notifications
Legacy Operations
date(withNaturalLanguageString:)date(withNaturalLanguageString:locale:)date(with:)init(string:)addTimeInterval(_:)date(withCalendarFormat:timeZone:)description(withCalendarFormat:timeZone:locale:)