- Inherits from:
- NSObject
- Package:
- com.apple.yellow.foundation
An NSDate object stores a date and time that can be compared to other dates and times.
earlierDate | Compares the receiver to the argument and returns the earlier of the two. |
isEqualToDate | Returns true if the receiver and the argument are equal. |
laterDate | Compares the receiver to the argument and returns the later of the two. |
timeIntervalSinceNow | Returns the number of seconds difference between the receiver and the current date and time. |
timeIntervalSinceReferenceDate |
NSDate objects represent a single point in time. NSDate declares the programmatic interface for specific and relative time values.
The objects you create using NSDate are referred to as date objects. They are immutable objects.
Generally, you instantiate a suitable date object by invoking one of the date... class methods.
NSDate is an abstract class that provides behavior for creating dates, comparing dates, representing dates, computing intervals, and similar functionality. It presents a programmatic interface through which suitable date objects are requested and returned. Date objects returned from NSDate are lightweight and immutable since they represent a invariant point in time. This class is designed to provide the foundation for arbitrary calendrical representations. Its subclass NSGregorianDate offers date objects that are suitable for representing dates according to western calendrical systems.
"Date" as used above implies clock time as well. The standard unit of time for date objects is a value typed as double and expressed as seconds. The double type makes possible a wide and fine-grained range of date and time values, giving accuracy within milliseconds for dates 10,000 years apart.
NSDate and its subclasses compute time as seconds relative to an absolute reference date. This reference date is the first instant of 1 January, 2001, Greenwich Mean Time (GMT). NSDate converts all date and time representations to and from NSTimeInterval values that are relative to this absolute reference date. A positive interval relative to a date represents a point in the future, a negative interval represents a time in the past.
Note: Cocoa implements time according to the Network Time Protocol (NTP) standard, which is based on Coordinated Universal Time. The current private implementations of NSDate follow the NTP standard. However, they do not account for leap seconds and therefore are not synchronized with International Atomic Time (the most accurate). |
Like various other Foundation classes, NSDate enables you to obtain operating-system functionality (dates and times) without depending on operating-system internals. It also provides a basis for the NSRunLoop and NSTimer classes, which use concrete date objects to implement local event loops and timers.
NSDate's sole primitive method, timeIntervalSinceReferenceDate, provides the basis for all the other methods in the NSDate interface. It returns a time value relative to an absolute reference date.
Use a date object to store a point in time. If you want to store the current time, use the date class method to create the date object. If you want to store some time other than the current time, use one of the dateWithTimeInterval... methods.
The dateWithTimeInterval... methods create date objects relative to a particular time, which the method name describes. You specify (in seconds) how much more recent or how much more in the past you want your date object to be. To specify a date that occurs earlier than the method's reference date, use a negative number of seconds.
The NSDate class provides, for your convenience, a public concrete subclass of NSDate that satisfies many requirements for dates and times. This subclass, NSGregorianDate, enables you to represent dates as arbitrary strings, to create new date objects from string representations, to extract date and time elements from date objects, and to do other calendar-related functions.
To obtain the difference between a date object and another point in time, send a timeInterval... message to the date object. For instance, timeIntervalSinceNow gives you the time, in seconds, between the current time and the receiving date object.
To compare dates, use the isEqualToDate, compare, laterDate, and earlierDate methods. These methods perform exact comparisons, which means they will detect subsecond differences between dates. You might want to compare dates with a less fine granularity. For example, you might want to consider two dates equal if they are within a minute of each other. If this is the case, use timeIntervalSinceDate to compare the two dates or use NSGregorianDate objects instead.
To represent your date object as a string, use the toString method.
If you want to subclass NSDate to obtain behavior different than that provided by the private subclasses, you must do these things:
Your subclass may use a different reference date than the absolute reference date used by NSDate (the first instance of 1 January 2001, GMT). If it does, it must still use the absolute reference date in its implementations of the method 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.
NSDate provides the following constants as a convenience:
Constant | Type | Description |
DateFor1970 |
NSDate | |
TimeIntervalSince1970 |
double | 978307200 |
- Constructors
- NSDate
- Creating an NSDate instance
- dateByAddingTimeInterval
- distantFuture
- distantPast
- Comparing dates
- isEqualToDate
- earlierDate
- laterDate
- compare
- equals
- Getting time intervals
- timeIntervalSinceDate
- timeIntervalSinceNow
- currentTimeIntervalSinceReferenceDate
- timeIntervalSinceReferenceDate
- Representing dates as Strings
- toString
- Working with milliseconds
- millisecondsToTimeInterval
- timeIntervalToMilliseconds
public NSDate()
public NSDate(double aDouble)
public NSDate(
double aDouble,
NSDate aNSDate)
public static double currentTimeIntervalSinceReferenceDate()
See Also: timeIntervalSinceReferenceDate, timeIntervalSinceDate, timeIntervalSinceNow
public static NSDate distantFuture()
You
can pass this value where an NSDate is required to have the date
argument essentially ignored. For example, the NSWindow method nextEventMatchingMask:untilDate:inMode:dequeue: returns null
if
an event specified in the event mask does not happen before the
specified date. You can use the object returned by distantFuture as
the date argument to wait indefinitely for the event to occur.
See Also: distantPast
public static NSDate distantPast()
See Also: distantFuture
public static double millisecondsToTimeInterval(long aLong)
public static long timeIntervalToMilliseconds(double aDouble)
public int compare(NSDate anotherDate)
OrderedSame
.
If the receiving object in the comparison is more recent than anotherDate,
the method returns OrderedDescending
.
If it is older, it returns OrderedAscending
. This method detects subsecond differences between dates. If you want to compare dates with a less fine granularity, use timeIntervalSinceDate to compare the two dates or use NSGregorianDate objects instead.
See Also: earlierDate, laterDate
public NSDate dateByAddingTimeInterval(double aDouble)
public NSDate earlierDate(NSDate anotherDate)
public boolean equals(Object anObject)
public int hashCode()
public boolean isEqualToDate(NSDate anotherDate)
See Also: compare, earlierDate, laterDate
public NSDate laterDate(NSDate anotherDate)
See Also: compare, earlierDate
public double timeIntervalSinceDate(NSDate anotherDate)
See Also: timeIntervalSinceNow, currentTimeIntervalSinceReferenceDate
public double timeIntervalSinceNow()
See Also: timeIntervalSinceDate, currentTimeIntervalSinceReferenceDate
public double timeIntervalSinceReferenceDate()
This is the primitive method for NSDate. If you subclass NSDate, you must override this method with your own implementation for it.
See Also: timeIntervalSinceDate, timeIntervalSinceNow, currentTimeIntervalSinceReferenceDate
public String toString()