NSDateComponents Class Reference
| Inherits from | |
| Conforms to | |
| Framework | /System/Library/Frameworks/Foundation.framework |
| Availability | Available in OS X v10.4 and later. |
| Companion guide | |
| Declared in | NSCalendar.h |
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.
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, Date Components, and Calendar Units” in Date and Time Programming Guide.
Tasks
Getting Information About an NSDateComponents Object
-
– era -
– year -
– month -
– date -
– day -
– hour -
– minute -
– second -
– week -
– weekday -
– weekdayOrdinal -
– quarter -
– calendar -
– timeZone -
– weekOfMonth -
– weekOfYear -
– yearForWeekOfYear
Setting Information for an NSDateComponents Object
Instance Methods
calendar
Returns the calendar of the receiver.
Return Value
The calendar.
Discussion
This value is interpreted in the context of the calendar with which it is used—see “Calendars, Date Components, and Calendar Units” in Date and Time Programming Guide.
Availability
- Available in OS X v10.7 and later.
See Also
Declared In
NSCalendar.hdate
Returns the date of the receiver.
Return Value
The date.
Availability
- Available in OS X v10.7 and later.
Declared In
NSCalendar.hday
Returns the number of day units for the receiver.
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, Date Components, and Calendar Units” in Date and Time Programming Guide.
Availability
- Available in OS X v10.4 and later.
See Also
Declared In
NSCalendar.hera
Returns the number of era units for the receiver.
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, Date Components, and Calendar Units” in Date and Time Programming Guide.
Availability
- Available in OS X v10.4 and later.
See Also
Declared In
NSCalendar.hhour
Returns the number of hour units for the receiver.
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, Date Components, and Calendar Units” in Date and Time Programming Guide.
Availability
- Available in OS X v10.4 and later.
See Also
Declared In
NSCalendar.hisLeapMonth
Returns whether the month is a leap month.
Return Value
YES if the month is a leap month, NO otherwise.
Availability
- Available in OS X v10.8 and later.
See Also
Declared In
NSCalendar.hminute
Returns the number of minute units for the receiver.
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, Date Components, and Calendar Units” in Date and Time Programming Guide.
Availability
- Available in OS X v10.4 and later.
See Also
Declared In
NSCalendar.hmonth
Returns the number of month units for the receiver.
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, Date Components, and Calendar Units” in Date and Time Programming Guide.
Availability
- Available in OS X v10.4 and later.
See Also
Declared In
NSCalendar.hquarter
Returns the number of quarters in the calendar.
Return Value
The number of quarters units for the receiver.
Availability
- Available in OS X v10.6 and later.
Declared In
NSCalendar.hsecond
Returns the number of second units for the receiver.
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, Date Components, and Calendar Units” in Date and Time Programming Guide.
Availability
- Available in OS X v10.4 and later.
See Also
Declared In
NSCalendar.hsetCalendar:
Sets the receiver’s calendar.
Parameters
- cal
The calendar.
Discussion
This value is interpreted in the context of the calendar with which it is used—see “Calendars, Date Components, and Calendar Units” in Date and Time Programming Guide.
Availability
- Available in OS X v10.7 and later.
See Also
Declared In
NSCalendar.hsetDay:
Sets the number of day units for the receiver.
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, Date Components, and Calendar Units” in Date and Time Programming Guide.
Availability
- Available in OS X v10.4 and later.
See Also
Declared In
NSCalendar.hsetEra:
Sets the number of era units for the receiver.
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, Date Components, and Calendar Units” in Date and Time Programming Guide.
Availability
- Available in OS X v10.4 and later.
See Also
Declared In
NSCalendar.hsetHour:
Sets the number of hour units for the receiver.
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, Date Components, and Calendar Units” in Date and Time Programming Guide.
Availability
- Available in OS X v10.4 and later.
See Also
Declared In
NSCalendar.hsetLeapMonth:
Sets the month as a leap month.
Parameters
- lm
YESif the month is a leap month,NOotherwise.
Availability
- Available in OS X v10.8 and later.
See Also
Declared In
NSCalendar.hsetMinute:
Sets the number of minute units for the receiver.
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, Date Components, and Calendar Units” in Date and Time Programming Guide.
Availability
- Available in OS X v10.4 and later.
See Also
Declared In
NSCalendar.hsetMonth:
Sets the number of month units for the receiver.
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, Date Components, and Calendar Units” in Date and Time Programming Guide.
Availability
- Available in OS X v10.4 and later.
See Also
Declared In
NSCalendar.hsetQuarter:
Sets the number of quarters in the calendar.
Parameters
- v
The number of quarters units for the receiver.
Discussion
This value is interpreted in the context of the calendar with which it is used—see “Calendars, Date Components, and Calendar Units” in Date and Time Programming Guide.
Availability
- Available in OS X v10.6 and later.
Declared In
NSCalendar.hsetSecond:
Sets the number of second units for the receiver.
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, Date Components, and Calendar Units” in Date and Time Programming Guide.
Availability
- Available in OS X v10.4 and later.
See Also
Declared In
NSCalendar.hsetTimeZone:
Sets the receiver’s time zone.
Parameters
- tz
The time zone.
Discussion
This value is interpreted in the context of the calendar with which it is used—see “Calendars, Date Components, and Calendar Units” in Date and Time Programming Guide.
Availability
- Available in OS X v10.7 and later.
See Also
Declared In
NSCalendar.hsetWeek:
Sets the number of week units for the receiver.
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, Date Components, and Calendar Units” in Date and Time Programming Guide.
Availability
- Available in OS X v10.4 and later.
See Also
Declared In
NSCalendar.hsetWeekday:
Sets the number of weekday units for the receiver.
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, Date Components, and Calendar Units” in Date and Time Programming Guide.
Availability
- Available in OS X v10.4 and later.
See Also
Declared In
NSCalendar.hsetWeekdayOrdinal:
Sets the ordinal number of weekday units for the receiver.
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, Date Components, and Calendar Units” in Date and Time Programming Guide.
Availability
- Available in OS X v10.4 and later.
See Also
Declared In
NSCalendar.hsetWeekOfMonth:
Sets the week of the month.
Parameters
- week
The week number of the month.
Discussion
This value is interpreted in the context of the calendar with which it is used—see “Calendars, Date Components, and Calendar Units” in Date and Time Programming Guide.
Availability
- Available in OS X v10.7 and later.
See Also
Declared In
NSCalendar.hsetWeekOfYear:
Sets the week of the year.
Parameters
- week
The week number of the year.
Discussion
This value is interpreted in the context of the calendar with which it is used—see “Calendars, Date Components, and Calendar Units” in Date and Time Programming Guide.
Availability
- Available in OS X v10.7 and later.
See Also
Declared In
NSCalendar.hsetYear:
Sets the number of year units for the receiver.
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, Date Components, and Calendar Units” in Date and Time Programming Guide.
Availability
- Available in OS X v10.4 and later.
See Also
Declared In
NSCalendar.hsetYearForWeekOfYear:
Sets the year for the week of the year.
Parameters
- year
The year when the calendar is being interpreted as a week-based calendar.
Discussion
This value is interpreted in the context of the calendar with which it is used—see “Calendars, Date Components, and Calendar Units” in Date and Time Programming Guide.
Availability
- Available in OS X v10.7 and later.
See Also
-
– yearForWeekOfYear -
firstWeekday(NSCalendar Class Reference) -
minimumDaysInFirstWeek(NSCalendar Class Reference)
Declared In
NSCalendar.htimeZone
Returns the receiver’s time zone.
Return Value
The time zone.
Discussion
This value is interpreted in the context of the calendar with which it is used—see “Calendars, Date Components, and Calendar Units” in Date and Time Programming Guide.
Availability
- Available in OS X v10.7 and later.
See Also
Declared In
NSCalendar.hweek
Returns the number of week units for the receiver.
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, Date Components, and Calendar Units” in Date and Time Programming Guide.
Availability
- Available in OS X v10.4 and later.
See Also
Declared In
NSCalendar.hweekday
Returns the number of weekday units for the receiver.
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, Date Components, and Calendar Units” in Date and Time Programming Guide.
Availability
- Available in OS X v10.4 and later.
See Also
Declared In
NSCalendar.hweekdayOrdinal
Returns the ordinal number of weekday units for the receiver.
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, Date Components, and Calendar Units” in Date and Time Programming Guide.
Availability
- Available in OS X v10.4 and later.
See Also
Declared In
NSCalendar.hweekOfMonth
Returns the week of the month.
Return Value
The week number of the month.
Discussion
This value is interpreted in the context of the calendar with which it is used—see “Calendars, Date Components, and Calendar Units” in Date and Time Programming Guide.
Availability
- Available in OS X v10.7 and later.
See Also
Declared In
NSCalendar.hweekOfYear
Returns the week of the year.
Return Value
The week number of the year.
Discussion
This value is interpreted in the context of the calendar with which it is used—see “Calendars, Date Components, and Calendar Units” in Date and Time Programming Guide.
Availability
- Available in OS X v10.7 and later.
Declared In
NSCalendar.hyear
Returns the number of year units for the receiver.
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, Date Components, and Calendar Units” in Date and Time Programming Guide.
Availability
- Available in OS X v10.4 and later.
See Also
Declared In
NSCalendar.hyearForWeekOfYear
Returns the year for the week of the year.
Return Value
The year number for the week of the year.
Discussion
This value is interpreted in the context of the calendar with which it is used—see “Calendars, Date Components, and Calendar Units” in Date and Time Programming Guide.
Availability
- Available in OS X v10.7 and later.
Declared In
NSCalendar.hConstants
NSDateComponents undefined component identifier
This constant specifies that an NSDateComponents component is undefined.
enum {
NSUndefinedDateComponent = 0x7fffffff
};
Constants
NSUndefinedDateComponentSpecifies that the component is undefined.
Available in OS X v10.4 and later.
Declared in
NSCalendar.h.
Declared In
NSCalendar.h© 2012 Apple Inc. All Rights Reserved. (Last updated: 2012-07-17)