Class

NSDate

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

Declaration

@interface 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 NSDateFormatter 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, CFDateRef. 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 initWithTimeIntervalSinceReferenceDate:, 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 initWithTimeIntervalSinceReferenceDate:. 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

Creating a Date

+ date

Creates and returns a new date object set to the current date and time.

+ dateWithTimeIntervalSinceNow:

Creates and returns a date object set to a given number of seconds from the current date and time.

+ dateWithTimeInterval:sinceDate:

Creates and returns a date object set to a given number of seconds from the specified date.

+ dateWithTimeIntervalSinceReferenceDate:

Creates and returns a date object set to a given number of seconds from 00:00:00 UTC on 1 January 2001.

+ dateWithTimeIntervalSince1970:

Creates and returns a date object set to the given number of seconds from 00:00:00 UTC on 1 January 1970.

Initializing a Date

- init

Returns a date object initialized to the current date and time.

- initWithTimeIntervalSinceNow:

Returns a date object initialized relative to the current date and time by a given number of seconds.

- initWithTimeInterval:sinceDate:

Returns a date object initialized relative to another given date by a given number of seconds.

- initWithTimeIntervalSinceReferenceDate:

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

- initWithTimeIntervalSince1970:

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

- initWithCoder:

Returns a date object initialized from data in the given unarchiver.

Getting Temporal Boundaries

distantFuture

A date object representing a date in the distant future.

distantPast

A date object representing a date in the distant past.

Comparing Dates

- isEqualToDate:

Returns a Boolean value that indicates whether a given object is a date that is exactly equal the receiver.

- earlierDate:

Returns the earlier of the receiver and another given date.

- laterDate:

Returns the later of the receiver and another given date.

- compare:

Indicates the temporal ordering of the receiver and another given date.

Getting Time Intervals

- timeIntervalSinceDate:

Returns the interval between the receiver and another given date.

timeIntervalSinceNow

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

timeIntervalSinceReferenceDate

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

timeIntervalSince1970

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

timeIntervalSinceReferenceDate

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

NSTimeIntervalSince1970

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

Adding Time Intervals

- dateByAddingTimeInterval:

Returns a new date object that is set to a given number of seconds relative to the receiver.

Describing Dates

description

A string representation of the date object.

- descriptionWithLocale:

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

Recognizing Notifications

NSSystemClockDidChangeNotification

A notification posted whenever the system clock is changed.

Legacy Operations

+ dateWithNaturalLanguageString:

Creates and returns a date object set to the date and time specified by a given string.

Deprecated
+ dateWithNaturalLanguageString:locale:

Creates and returns a date object set to the date and time specified by a given string.

Deprecated
+ dateWithString:

Creates and returns a date object with a date and time value specified by a given string in the international string representation format (YYYY-MM-DD HH:MM:SS ±HHMM).

Deprecated
- initWithString:

Returns a date object initialized with a date and time value specified by a given string in the international string representation format.

Deprecated
- addTimeInterval:

Returns a new date object that is set to a given number of seconds relative to the receiver.

Deprecated
- dateWithCalendarFormat:timeZone:

Converts the receiver to a calendar date with a given format string and time zone.

Deprecated
- descriptionWithCalendarFormat:timeZone:locale:

Returns a string representation of the date formatted as specified by given conversion specifiers.

Deprecated

Type Properties

Relationships

Inherits From

See Also

Date Representations

NSDateInterval

An object representing the span of time between a specific start date and end date.

NSTimeInterval

A number of seconds.