Class: NSDate
Overview
NSDate objects represent a single point in time. NSDate is a class cluster; its single public superclass, 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. Because of the nature of class clusters, objects returned by the NSDate class are instances not of that abstract class but of one of its private subclasses. Although a date object’s class is private, its interface is public, as declared by the abstract superclass NSDate. Generally, you instantiate a suitable date object by invoking one of the date… class methods.
Direct Known Subclasses
Class Method Summary (collapse)
-
+ date
Creates and returns a new date set to the current date and time.
-
+ dateWithTimeInterval:sinceDate:
Creates and returns an NSDate object set to a given number of seconds from the specified date.
-
+ dateWithTimeIntervalSince1970:
Creates and returns an NSDate object set to the given number of seconds from the first instant of 1 January 1970, GMT.
-
+ dateWithTimeIntervalSinceNow:
Creates and returns an NSDate object set to a given number of seconds from the current date and time.
-
+ dateWithTimeIntervalSinceReferenceDate:
Creates and returns an NSDate object set to a given number of seconds from the first instant of 1 January 2001, GMT.
-
+ distantFuture
Creates and returns an NSDate object representing a date in the distant future.
-
+ distantPast
Creates and returns an NSDate object representing a date in the distant past.
-
+ timeIntervalSinceReferenceDate
Returns the interval between the first instant of 1 January 2001, GMT and the current date and time.
Instance Method Summary (collapse)
-
- compare:
Returns an NSComparisonResult value that indicates the temporal ordering of the receiver and another given date.
-
- dateByAddingTimeInterval:
Returns a new NSDate object that is set to a given number of seconds relative to the receiver.
-
- description
Returns a string representation of the receiver.
-
- descriptionWithLocale:
Returns a string representation of the receiver using the given locale.
-
- earlierDate:
Returns the earlier of the receiver and another given date.
-
- init
Returns an NSDate object initialized to the current date and time.
-
- initWithTimeInterval:sinceDate:
Returns an NSDate object initialized relative to another given date by a given number of seconds.
-
- initWithTimeIntervalSince1970:
Returns an NSDate object set to the given number of seconds from the first instant of 1 January 1970, GMT.
-
- initWithTimeIntervalSinceNow:
Returns an NSDate object initialized relative to the current date and time by a given number of seconds.
-
- initWithTimeIntervalSinceReferenceDate:
Returns an NSDate object initialized relative the first instant of 1 January 2001, GMT by a given number of seconds.
-
- isEqualToDate:
Returns a Boolean value that indicates whether a given object is an NSDate object and exactly equal the receiver.
-
- laterDate:
Returns the later of the receiver and another given date.
-
- timeIntervalSince1970
Returns the interval between the receiver and the first instant of 1 January 1970, GMT.
-
- timeIntervalSinceDate:
Returns the interval between the receiver and another given date.
-
- timeIntervalSinceNow
Returns the interval between the receiver and the current date and time.
-
- timeIntervalSinceReferenceDate
Returns the interval between the receiver and the first instant of 1 January 2001, GMT.
Methods inherited from NSObject
#!, #!=, #!~, #, #==, #===, #=~, #Rational, #__callee__, #__method__, #__send__, #__type__, `, alloc, allocWithZone:, #autoContentAccessingProxy, autoload, autoload?, autorelease_pool, #awakeAfterUsingCoder:, binding, block_given?, caller, cancelPreviousPerformRequestsWithTarget:, cancelPreviousPerformRequestsWithTarget:selector:object:, catch, class, classFallbacksForKeyedArchiver, #classForCoder, #classForKeyedArchiver, classForKeyedUnarchiver, #clone, conformsToProtocol:, #copy, copyWithZone:, #dealloc, #define_singleton_method, description, display, #doesNotRecognizeSelector:, #dup, #enum_for, #eql?, #equal?, #extend, fail, #finalize, format, #forwardInvocation:, #forwardingTargetForSelector:, framework, #freeze, #frozen?, getpass, gets, global_variables, initialize, #initialize_clone, #initialize_copy, #initialize_dup, #inspect, instanceMethodForSelector:, instanceMethodSignatureForSelector:, #instance_eval, #instance_exec, #instance_of?, #instance_variable_defined?, #instance_variable_get, #instance_variable_set, #instance_variables, instancesRespondToSelector:, isSubclassOfClass:, #is_a?, iterator?, #kind_of?, lambda, load, load_bridge_support_file, load_plist, local_variables, loop, #method, #methodForSelector:, #methodSignatureForSelector:, #methods, #mutableCopy, mutableCopyWithZone:, new, #nil?, open, p, #performSelector:onThread:withObject:waitUntilDone:, #performSelector:onThread:withObject:waitUntilDone:modes:, #performSelector:withObject:afterDelay:, #performSelector:withObject:afterDelay:inModes:, #performSelectorInBackground:withObject:, #performSelectorOnMainThread:withObject:waitUntilDone:, #performSelectorOnMainThread:withObject:waitUntilDone:modes:, print, printf, #private_methods, proc, #protected_methods, #public_method, #public_methods, #public_send, putc, puts, raise, rand, readline, readlines, #replacementObjectForCoder:, #replacementObjectForKeyedArchiver:, require, resolveClassMethod:, resolveInstanceMethod:, #respond_to?, #respond_to_missing?, select, #send, setVersion:, #singleton_methods, sprintf, srand, superclass, #taint, #tainted?, #tap, test, throw, #to_plist, #to_s, trace_var, trap, #trust, #untaint, untrace_var, #untrust, #untrusted?, version
Constructor Details
This class inherits a constructor from NSObject
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class NSObject
Class Method Details
+ (Object) date
Creates and returns a new date set to the current date and time. This method uses the default initializer method for the class, init.The following code sample shows how to use date to get the current date:
+ (Object) dateWithTimeInterval(seconds, sinceDate:date)
Creates and returns an NSDate object set to a given number of seconds from the specified date.
+ (Object) dateWithTimeIntervalSince1970(seconds)
+ (Object) dateWithTimeIntervalSinceNow(seconds)
Creates and returns an NSDate object set to a given number of seconds from the current date and time.
+ (Object) dateWithTimeIntervalSinceReferenceDate(seconds)
Creates and returns an NSDate object set to a given number of seconds from the first instant of 1 January 2001, GMT.
+ (Object) distantFuture
Creates and returns an NSDate object representing a date in the distant future. You can pass this value when an NSDate object is required to have the date argument essentially ignored. For example, the NSWindow method nextEventMatchingMask:untilDate:inMode:dequeue: returns nil 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.
Instance Method Details
- (NSComparisonResult) compare(anotherDate)
Returns an NSComparisonResult value that indicates the temporal ordering of the receiver and another given date. This method detects sub-second differences between dates. If you want to compare dates with a less fine granularity, use timeIntervalSinceDate: to compare the two dates.
- (Object) dateByAddingTimeInterval(seconds)
Returns a new NSDate object that is set to a given number of seconds relative to the receiver.
- (String) description
Returns a string representation of the receiver. The representation is not guaranteed to remain constant across different releases of the operating system. To format a date, you should use a date formatter object instead (see NSDateFormatter and Data Formatting Guide)
- (String) descriptionWithLocale(locale)
Returns a string representation of the receiver using the given locale.
- (NSDate) earlierDate(anotherDate)
Returns the earlier of the receiver and another given date.
- (Object) init
Returns an NSDate object initialized to the current date and time. This method uses the designated initializer, initWithTimeIntervalSinceReferenceDate:.
- (Object) initWithTimeInterval(seconds, sinceDate:refDate)
Returns an NSDate object initialized relative to another given date by a given number of seconds. This method uses the designated initializer, initWithTimeIntervalSinceReferenceDate:.
- (Object) initWithTimeIntervalSince1970(seconds)
- (Object) initWithTimeIntervalSinceNow(seconds)
Returns an NSDate object initialized relative to the current date and time by a given number of seconds. This method uses the designated initializer, initWithTimeIntervalSinceReferenceDate:.
- (Object) initWithTimeIntervalSinceReferenceDate(seconds)
Returns an NSDate object initialized relative the first instant of 1 January 2001, GMT by a given number of seconds. This method is the designated initializer for the NSDate class and is declared primarily for the use of subclasses of NSDate. When you subclass NSDate to create a concrete date class, you must override this method.
- (Boolean) isEqualToDate(anotherDate)
Returns a Boolean value that indicates whether a given object is an NSDate object and exactly equal the receiver. This method detects sub-second differences between dates. If you want to compare dates with a less fine granularity, use timeIntervalSinceDate: to compare the two dates.
- (NSDate) laterDate(anotherDate)
Returns the later of the receiver and another given date.
- (NSTimeInterval) timeIntervalSince1970
Returns the interval between the receiver and the first instant of 1 January 1970, GMT.
- (NSTimeInterval) timeIntervalSinceDate(anotherDate)
Returns the interval between the receiver and another given date.
- (NSTimeInterval) timeIntervalSinceNow
Returns the interval between the receiver and the current date and time.
- (NSTimeInterval) timeIntervalSinceReferenceDate
Returns the interval between the receiver and the first instant of 1 January 2001, GMT.