A representation of a specific point in time that bridges to Date; use NSDate when you need reference semantics or other Foundation-specific behavior.
SDKs
- iOS 2.0+
- macOS 10.0+
- Mac Catalyst 13.0+
- tvOS 9.0+
- watchOS 2.0+
Framework
- Foundation
Declaration
class NSDate : NSObject
Overview
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 Date 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.
Important
The Swift overlay to the Foundation framework provides the Date structure, which bridges to the NSDate class. For more information about value types, see Working with Cocoa Frameworks in Using Swift with Cocoa and Objective-C (Swift 4.1).
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
timeinstance method to provide the correct date and time value based on your instance variableInterval Since Reference Date Override
init(time, one of the designated initializer methodsInterval Since Reference Date:) 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
NSCopyingand NSCoding protocols, becauseNSDateadopts 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 time and init(time. 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.