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 |
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.
– setEra:
– setYear:
– setMonth:
– setDay:
– setHour:
– setMinute:
– setSecond:
– setWeek:
– setWeekday:
– setWeekdayOrdinal:
Returns the number of day units for the receiver.
- (NSInteger)day
The number of day units for the receiver.
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.
NSCalendar.h
Returns the number of era units for the receiver.
- (NSInteger)era
The number of era units for the receiver.
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.
NSCalendar.h
Returns the number of hour units for the receiver.
- (NSInteger)hour
The number of hour units for the receiver.
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.
NSCalendar.h
Returns the number of minute units for the receiver.
- (NSInteger)minute
The number of minute units for the receiver.
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.
NSCalendar.h
Returns the number of month units for the receiver.
- (NSInteger)month
The number of month units for the receiver.
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.
NSCalendar.h
Returns the number of second units for the receiver.
- (NSInteger)second
The number of second units for the receiver.
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.
NSCalendar.h
Sets the number of day units for the receiver.
- (void)setDay:(NSInteger)v
The number of day units for the receiver.
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.
NSCalendar.h
Sets the number of era units for the receiver.
- (void)setEra:(NSInteger)v
The number of era units for the receiver.
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.
NSCalendar.h
Sets the number of hour units for the receiver.
- (void)setHour:(NSInteger)v
The number of hour units for the receiver.
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.
NSCalendar.h
Sets the number of minute units for the receiver.
- (void)setMinute:(NSInteger)v
The number of minute units for the receiver.
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.
NSCalendar.h
Sets the number of month units for the receiver.
- (void)setMonth:(NSInteger)v
The number of month units for the receiver.
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.
NSCalendar.h
Sets the number of second units for the receiver.
- (void)setSecond:(NSInteger)v
The number of second units for the receiver.
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.
NSCalendar.h
Sets the number of week units for the receiver.
- (void)setWeek:(NSInteger)v
The number of week units for the receiver.
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.
NSCalendar.h
Sets the number of weekday units for the receiver.
- (void)setWeekday:(NSInteger)v
The number of weekday units for the receiver.
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.
NSCalendar.h
Sets the ordinal number of weekday units for the receiver.
- (void)setWeekdayOrdinal:(NSInteger)v
The ordinal number of weekday units for the receiver.
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.
NSCalendar.h
Sets the number of year units for the receiver.
- (void)setYear:(NSInteger)v
The number of year units for the receiver.
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.
NSCalendar.h
Returns the number of week units for the receiver.
- (NSInteger)week
The number of week units for the receiver.
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.
NSCalendar.h
Returns the number of weekday units for the receiver.
- (NSInteger)weekday
The number of weekday units for the receiver.
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.
NSCalendar.h
Returns the ordinal number of weekday units for the receiver.
- (NSInteger)weekdayOrdinal
The ordinal number of weekday units for the receiver.
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.
NSCalendar.h
Returns the number of year units for the receiver.
- (NSInteger)year
The number of year units for the receiver.
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.
NSCalendar.h
This constant specifies that an NSDateComponents
component is undefined.
enum { NSUndefinedDateComponent = 0x7fffffff };
NSUndefinedDateComponent
Specifies that the component is undefined.
Available in Mac OS X v10.4 and later.
Declared in NSCalendar.h
.
NSCalendar.h
© 2008 Apple Inc. All Rights Reserved. (Last updated: 2008-10-15)