Class: NSDate

Inherits:
NSObject show all

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

Time

Class Method Summary (collapse)

Instance Method Summary (collapse)

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:

Returns:

  • (Object)

    A new date object set to the current date and time.

+ (Object) dateWithTimeInterval(seconds, sinceDate:date)

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

Parameters:

  • seconds (NSTimeInterval)

    The number of seconds to add to date. Use a negative argument to specify a date and time before date.

  • date (NSDate)

    The date.

Returns:

  • (Object)

    An NSDate object set to seconds seconds from date.

+ (Object) dateWithTimeIntervalSince1970(seconds)

Creates and returns an NSDate object set to the given number of seconds from the first instant of 1 January 1970, GMT. This method is useful for creating NSDate objects from time_t values returned by BSD system functions.

Parameters:

  • seconds (NSTimeInterval)

    The number of seconds from the reference date, 1 January 1970, GMT, for the new date. Use a negative argument to specify a date before this date.

Returns:

  • (Object)

    An NSDate object set to seconds seconds from the reference date.

+ (Object) dateWithTimeIntervalSinceNow(seconds)

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

Parameters:

  • seconds (NSTimeInterval)

    The number of seconds from the current date and time for the new date. Use a negative value to specify a date before the current date.

Returns:

  • (Object)

    An NSDate object set to seconds 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.

Parameters:

  • seconds (NSTimeInterval)

    The number of seconds from the absolute reference date (the first instant of 1 January 2001, GMT) for the new date. Use a negative argument to specify a date and time before the reference date.

Returns:

  • (Object)

    An NSDate object set to seconds seconds from the absolute reference date.

+ (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.

Returns:

  • (Object)

    An NSDate object representing a date in the distant future (in terms of centuries).

+ (Object) distantPast

Creates and returns an NSDate object representing a date in the distant past. You can use this object as a control date, a guaranteed temporal boundary.

Returns:

  • (Object)

    An NSDate object representing a date in the distant past (in terms of centuries).

+ (NSTimeInterval) timeIntervalSinceReferenceDate

Returns the interval between the first instant of 1 January 2001, GMT and the current date and time. This method is the primitive method for NSDate. If you subclass NSDate, you must override this method with your own implementation for it.

Returns:

  • (NSTimeInterval)

    The interval between the system’s absolute reference date (the first instant of 1 January 2001, GMT) and the current date and time.

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.

Parameters:

  • anotherDate (NSDate)

    The date with which to compare the receiver.This value must not be nil. If the value is nil, the behavior is undefined and may change in future versions of OS X.

Returns:

  • (NSComparisonResult)

    If:

- (Object) dateByAddingTimeInterval(seconds)

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

Parameters:

  • seconds (NSTimeInterval)

    The number of seconds to add to the receiver. Use a negative value for seconds to have the returned object specify a date before the receiver.

Returns:

  • (Object)

    A new NSDate object that is set to seconds seconds relative to the receiver. The date returned might have a representation different from the receiver’s.

- (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)

Returns:

  • (String)

    A string representation of the receiver.

- (String) descriptionWithLocale(locale)

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

Parameters:

  • locale (Object)

    An NSLocale object.If you pass nil, NSDate formats the date in the same way as the description method. On OS X v10.4 and earlier, this parameter was an NSDictionary object. If you pass in an NSDictionary object on OS X v10.5, NSDate uses the default user locale—the same as if you passed in [NSLocale currentLocale].

Returns:

  • (String)

    A string representation of the receiver, using the given locale, or if the locale argument is nil, in the international format YYYY-MM-DD HH:MM:SS ±HHMM, where ±HHMM represents the time zone offset in hours and minutes from GMT (for example, “2001-03-24 10:45:32 +0600”)

- (NSDate) earlierDate(anotherDate)

Returns the earlier of the receiver and another given date.

Parameters:

  • anotherDate (NSDate)

    The date with which to compare the receiver.

Returns:

  • (NSDate)

    The earlier of the receiver and anotherDate, determined using timeIntervalSinceDate:. If the receiver and anotherDate represent the same date, returns the receiver.

- (Object) init

Returns an NSDate object initialized to the current date and time. This method uses the designated initializer, initWithTimeIntervalSinceReferenceDate:.

Returns:

  • (Object)

    An NSDate object initialized to the current date and time.

- (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:.

Parameters:

  • seconds (NSTimeInterval)

    The number of seconds to add to refDate. A negative value means the receiver will be earlier than refDate.

  • refDate (NSDate)

    The reference date.

Returns:

  • (Object)

    An NSDate object initialized relative to refDate by seconds seconds.

- (Object) initWithTimeIntervalSince1970(seconds)

Returns an NSDate object set to the given number of seconds from the first instant of 1 January 1970, GMT. This method is useful for creating NSDate objects from time_t values returned by BSD system functions.

Parameters:

  • seconds (NSTimeInterval)

    The number of seconds from the reference date, 1 January 1970, GMT, for the new date. Use a negative argument to specify a date before this date.

Returns:

  • (Object)

    An NSDate object set to seconds seconds from the reference date.

- (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:.

Parameters:

  • seconds (NSTimeInterval)

    The number of seconds from relative to the current date and time to which the receiver should be initialized. A negative value means the returned object will represent a date in the past.

Returns:

  • (Object)

    An NSDate object initialized relative to the current date and time by seconds seconds.

- (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.

Parameters:

  • seconds (NSTimeInterval)

    The number of seconds to add to the reference date (the first instant of 1 January 2001, GMT). A negative value means the receiver will be earlier than the reference date.

Returns:

  • (Object)

    An NSDate object initialized relative to the absolute reference date by seconds seconds.

- (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.

Parameters:

  • anotherDate (NSDate)

    The date to compare with the receiver.

Returns:

  • (Boolean)

    YES if the anotherDate is an NSDate object and is exactly equal to the receiver, otherwise NO.

- (NSDate) laterDate(anotherDate)

Returns the later of the receiver and another given date.

Parameters:

  • anotherDate (NSDate)

    The date with which to compare the receiver.

Returns:

  • (NSDate)

    The later of the receiver and anotherDate, determined using timeIntervalSinceDate:. If the receiver and anotherDate represent the same date, returns the receiver.

- (NSTimeInterval) timeIntervalSince1970

Returns the interval between the receiver and the first instant of 1 January 1970, GMT.

Returns:

  • (NSTimeInterval)

    The interval between the receiver and the reference date, 1 January 1970, GMT. If the receiver is earlier than the reference date, the value is negative.

- (NSTimeInterval) timeIntervalSinceDate(anotherDate)

Returns the interval between the receiver and another given date.

Parameters:

  • anotherDate (NSDate)

    The date with which to compare the receiver.

Returns:

  • (NSTimeInterval)

    The interval between the receiver and anotherDate. If the receiver is earlier than anotherDate, the return value is negative.

- (NSTimeInterval) timeIntervalSinceNow

Returns the interval between the receiver and the current date and time.

Returns:

  • (NSTimeInterval)

    The interval between the receiver and the current date and time. If the receiver is earlier than the current date and time, the return value is negative.

- (NSTimeInterval) timeIntervalSinceReferenceDate

Returns the interval between the receiver and the first instant of 1 January 2001, GMT.

Returns:

  • (NSTimeInterval)

    The interval between the receiver and the system’s absolute reference date (the first instant of 1 January 2001, GMT). If the receiver is earlier than the reference date, the value is negative.