Next Page > Hide TOC

NSDateComponents Class Reference

Inherits from
Conforms to
Framework
/System/Library/Frameworks/Foundation.framework
Availability
Available in Mac OS X v10.4 and later.
Companion guide
Declared in
NSCalendar.h
Related sample code

Overview

NSDateComponents encapsulates the components of a date in an extendable, object-oriented manner. It is used to specify a date by providing the temporal components that make up a date and time: hour, minutes, seconds, day, month, year, and so on. It can also be used to specify a duration of time, for example, 5 hours and 16 minutes. An NSDateComponents object is not required to define all the component fields. When a new instance of NSDateComponents is created the date components are set to NSUndefinedDateComponent.

Important:  An NSDateComponents object is meaningless in itself; you need to know what calendar it is interpreted against, and you need to know whether the values are absolute values of the units, or quantities of the units.

An instance of NSDateComponents is not responsible for answering questions about a date beyond the information with which it was initialized. For example, if you initialize one with May 6, 2004, its weekday is NSUndefinedDateComponent, not Thursday. To get the correct day of the week, you must create a suitable instance of NSCalendar, create an NSDate object using dateFromComponents: and then use components:fromDate: to retrieve the weekday—as illustrated in the following example.

NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setDay:6];
[comps setMonth:5];
[comps setYear:2004];
NSCalendar *gregorian = [[NSCalendar alloc]
    initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *date = [gregorian dateFromComponents:comps];
[comps release];
NSDateComponents *weekdayComponents =
    [gregorian components:NSWeekdayCalendarUnit fromDate:date];
int weekday = [weekdayComponents weekday];

For more details, see Calendars in Date and Time Programming Guide for Cocoa.

Tasks

Getting Information About an NSDateComponents Object

Setting Information for an NSDateComponents Object

Instance Methods

day

Returns the number of day units for the receiver.

- (NSInteger)day

Return Value

The number of day units for the receiver.

Discussion

This value is interpreted in the context of the calendar with which it is used—see Calendars in Date and Time Programming Guide for Cocoa.

Availability
See Also
Related Sample Code
Declared In
NSCalendar.h

era

Returns the number of era units for the receiver.

- (NSInteger)era

Return Value

The number of era units for the receiver.

Discussion

This value is interpreted in the context of the calendar with which it is used—see Calendars in Date and Time Programming Guide for Cocoa.

Availability
See Also
Declared In
NSCalendar.h

hour

Returns the number of hour units for the receiver.

- (NSInteger)hour

Return Value

The number of hour units for the receiver.

Discussion

This value is interpreted in the context of the calendar with which it is used—see Calendars in Date and Time Programming Guide for Cocoa.

Availability
See Also
Declared In
NSCalendar.h

minute

Returns the number of minute units for the receiver.

- (NSInteger)minute

Return Value

The number of minute units for the receiver.

Discussion

This value is interpreted in the context of the calendar with which it is used—see Calendars in Date and Time Programming Guide for Cocoa.

Availability
See Also
Declared In
NSCalendar.h

month

Returns the number of month units for the receiver.

- (NSInteger)month

Return Value

The number of month units for the receiver.

Discussion

This value is interpreted in the context of the calendar with which it is used—see Calendars in Date and Time Programming Guide for Cocoa.

Availability
See Also
Declared In
NSCalendar.h

second

Returns the number of second units for the receiver.

- (NSInteger)second

Return Value

The number of second units for the receiver.

Discussion

This value is interpreted in the context of the calendar with which it is used—see Calendars in Date and Time Programming Guide for Cocoa.

Availability
See Also
Declared In
NSCalendar.h

setDay:

Sets the number of day units for the receiver.

- (void)setDay:(NSInteger)v

Parameters
v

The number of day units for the receiver.

Discussion

This value will be interpreted in the context of the calendar with which it is used—see Calendars in Date and Time Programming Guide for Cocoa.

Availability
See Also
Related Sample Code
Declared In
NSCalendar.h

setEra:

Sets the number of era units for the receiver.

- (void)setEra:(NSInteger)v

Parameters
v

The number of era units for the receiver.

Discussion

This value will be interpreted in the context of the calendar with which it is used—see Calendars in Date and Time Programming Guide for Cocoa.

Availability
See Also
Declared In
NSCalendar.h

setHour:

Sets the number of hour units for the receiver.

- (void)setHour:(NSInteger)v

Parameters
v

The number of hour units for the receiver.

Discussion

This value will be interpreted in the context of the calendar with which it is used—see Calendars in Date and Time Programming Guide for Cocoa.

Availability
See Also
Related Sample Code
Declared In
NSCalendar.h

setMinute:

Sets the number of minute units for the receiver.

- (void)setMinute:(NSInteger)v

Parameters
v

The number of minute units for the receiver.

Discussion

This value will be interpreted in the context of the calendar with which it is used—see Calendars in Date and Time Programming Guide for Cocoa.

Availability
See Also
Related Sample Code
Declared In
NSCalendar.h

setMonth:

Sets the number of month units for the receiver.

- (void)setMonth:(NSInteger)v

Parameters
v

The number of month units for the receiver.

Discussion

This value will be interpreted in the context of the calendar with which it is used—see Calendars in Date and Time Programming Guide for Cocoa.

Availability
See Also
Related Sample Code
Declared In
NSCalendar.h

setSecond:

Sets the number of second units for the receiver.

- (void)setSecond:(NSInteger)v

Parameters
v

The number of second units for the receiver.

Discussion

This value will be interpreted in the context of the calendar with which it is used—see Calendars in Date and Time Programming Guide for Cocoa.

Availability
See Also
Related Sample Code
Declared In
NSCalendar.h

setWeek:

Sets the number of week units for the receiver.

- (void)setWeek:(NSInteger)v

Parameters
v

The number of week units for the receiver.

Discussion

This value will be interpreted in the context of the calendar with which it is used—see Calendars in Date and Time Programming Guide for Cocoa.

Availability
See Also
Declared In
NSCalendar.h

setWeekday:

Sets the number of weekday units for the receiver.

- (void)setWeekday:(NSInteger)v

Parameters
v

The number of weekday units for the receiver.

Discussion

Weekday units are the numbers 1 through n, where n is the number of days in the week. For example, in the Gregorian calendar, n is 7 and Sunday is represented by 1.

This value will be interpreted in the context of the calendar with which it is used—see Calendars in Date and Time Programming Guide for Cocoa.

Availability
See Also
Declared In
NSCalendar.h

setWeekdayOrdinal:

Sets the ordinal number of weekday units for the receiver.

- (void)setWeekdayOrdinal:(NSInteger)v

Parameters
v

The ordinal number of weekday units for the receiver.

Discussion

Weekday ordinal units represent the position of the weekday within the next larger calendar unit, such as the month. For example, 2 is the weekday ordinal unit for the second Friday of the month.

This value will be interpreted in the context of the calendar with which it is used—see Calendars in Date and Time Programming Guide for Cocoa.

Availability
See Also
Declared In
NSCalendar.h

setYear:

Sets the number of year units for the receiver.

- (void)setYear:(NSInteger)v

Parameters
v

The number of year units for the receiver.

Discussion

This value will be interpreted in the context of the calendar with which it is used—see Calendars in Date and Time Programming Guide for Cocoa.

Availability
See Also
Related Sample Code
Declared In
NSCalendar.h

week

Returns the number of week units for the receiver.

- (NSInteger)week

Return Value

The number of week units for the receiver.

Discussion

This value is interpreted in the context of the calendar with which it is used—see Calendars in Date and Time Programming Guide for Cocoa.

Availability
See Also
Declared In
NSCalendar.h

weekday

Returns the number of weekday units for the receiver.

- (NSInteger)weekday

Return Value

The number of weekday units for the receiver.

Discussion

Weekday units are the numbers 1 through n, where n is the number of days in the week. For example, in the Gregorian calendar, n is 7 and Sunday is represented by 1.

This value is interpreted in the context of the calendar with which it is used—see Calendars in Date and Time Programming Guide for Cocoa.

Availability
See Also
Related Sample Code
Declared In
NSCalendar.h

weekdayOrdinal

Returns the ordinal number of weekday units for the receiver.

- (NSInteger)weekdayOrdinal

Return Value

The ordinal number of weekday units for the receiver.

Discussion

Weekday ordinal units represent the position of the weekday within the next larger calendar unit, such as the month. For example, 2 is the weekday ordinal unit for the second Friday of the month.

This value is interpreted in the context of the calendar with which it is used—see Calendars in Date and Time Programming Guide for Cocoa.

Availability
See Also
Declared In
NSCalendar.h

year

Returns the number of year units for the receiver.

- (NSInteger)year

Return Value

The number of year units for the receiver.

Discussion

This value is interpreted in the context of the calendar with which it is used—see Calendars in Date and Time Programming Guide for Cocoa.

Availability
See Also
Declared In
NSCalendar.h

Constants

NSDateComponents undefined component identifier

This constant specifies that an NSDateComponents component is undefined.

enum {
   NSUndefinedDateComponent = 0x7fffffff
};

Constants
NSUndefinedDateComponent

Specifies that the component is undefined.

Available in Mac OS X v10.4 and later.

Declared in NSCalendar.h.

Declared In
NSCalendar.h

Next Page > Hide TOC


© 2008 Apple Inc. All Rights Reserved. (Last updated: 2008-10-15)


Did this document help you?
Yes: Tell us what works for you.
It’s good, but: Report typos, inaccuracies, and so forth.
It wasn’t helpful: Tell us what would have helped.