Class

NSCalendar

An object that defines the relationships between calendar units (such as eras, years, and weekdays) and absolute points in time, providing features for calculation and comparison of dates.

Overview

NSCalendar objects encapsulate information about systems of reckoning time in which the beginning, length, and divisions of a year are defined. They provide information about the calendar and support for calendrical computations such as determining the range of a given calendrical unit and adding units to a given absolute time.

NSCalendar is “toll-free bridged” with its Core Foundation counterpart, CFCalendarRef. See Toll-Free Bridging for more information on toll-free bridging.

Locales and Calendars

Most locales use the most widely used civil calendar, called the Gregorian calendar (NSCalendarIdentifierGregorian), but there remain exceptions to this trend. For example:

Other locales use another calendar alongside the Gregorian calendar. For example:

Independent of any particular locale, certain calendars are used primarily to calculate dates for religious observances. Among these are the Buddhist (NSCalendarIdentifierBuddhist), Coptic (NSCalendarIdentifierCoptic), Hebrew (NSCalendarIdentifierHebrew), and Islamic (NSCalendarIdentifierIslamic) calendars.

How NSCalendar Models the Gregorian Calendar

The Gregorian calendar was first introduced in 1582, as a replacement for the Julian Calendar. According to the Julian calendar, a leap day is added to February for any year with a number divisible by 4, which results in an annual disparity of 11 minutes, or 1 day every 128 years. The Gregorian calendar revised the rules for leap day calculation, by skipping the leap day for any year with a number divisible by 100, unless that year number is also divisible by 400, resulting in an annual disparity of only 26 seconds, or 1 day every 3323 years.

To transition from the Julian calendar to the Gregorian calendar, 10 days were dropped from the Gregorian calendar (October 5–14).

After the Gregorian calendar was introduced, many countries continued to use the Julian calendar, with Turkey being the last country to adopt the Gregorian calendar, in 1926. As a result of the staggered adoption, the transition period for countries at the time of adoption have different start dates and a different number of skipped days to account for the additional disparity from leap day calculations.

NSCalendar models the behavior of a proleptic Gregorian calendar (as defined by ISO 8601:2004), which extends the Gregorian calendar backward in time from the date of its introduction. This behavior should be taken into account when working with dates created before the transition period of the affected locales.

Calendar Arithmetic

To do calendar arithmetic, you use NSDate objects in conjunction with a calendar. For example, to convert between a decomposed date in one calendar and another calendar, you must first convert the decomposed elements into a date using the first calendar, then decompose it using the second. NSDate provides the absolute scale and epoch (reference point) for dates and times, which can then be rendered into a particular calendar, for calendrical computations or user display.

Two NSCalendar methods that return a date object, dateFromComponents:, dateByAddingComponents:toDate:options:, take as a parameter an NSDateComponents object that describes the calendrical components required for the computation. You can provide as many components as you need (or choose to). When there is incomplete information to compute an absolute time, default values similar to 0 and 1 are usually chosen by a calendar, but this is a calendar-specific choice. If you provide inconsistent information, calendar-specific disambiguation is performed (which may involve ignoring one or more of the parameters). Related methods (components:fromDate: and components:fromDate:toDate:options:) take a bit mask parameter that specifies which components to calculate when returning an NSDateComponents object. The bit mask is composed of NSCalendarUnit constants (see Constants).

In a calendar, day, week, weekday, month, and year numbers are generally 1-based, but there may be calendar-specific exceptions. Ordinal numbers, where they occur, are 1-based. Some calendars represented by this API may have to map their basic unit concepts into year/month/week/day/… nomenclature. For example, a calendar composed of 4 quarters in a year instead of 12 months uses the month unit to represent quarters. The particular values of the unit are defined by each calendar, and are not necessarily consistent with values for that unit in another calendar.

Topics

Creating and Initializing Calendars

calendarWithIdentifier:

Creates a new calendar specified by a given identifier.

initWithCalendarIdentifier:

Initializes a calendar according to a given identifier.

NSCalendarIdentifier

The supported calendar types.

Getting the User's Calendar

autoupdatingCurrentCalendar

A calendar that tracks changes to user’s preferred calendar.

currentCalendar

The user’s current calendar.

Extracting Components

date:matchesComponents:

Returns whether a given date matches all of the given date components.

component:fromDate:

Returns the specified date component from a given date.

components:fromDate:

Returns the date components representing a given date.

components:fromDate:toDate:options:

Returns the difference between two supplied dates as date components.

components:fromDateComponents:toDateComponents:options:

Returns the difference between start and end dates given as date components.

componentsInTimeZone:fromDate:

Returns all the date components of a date, as if in a given time zone (instead of the receiving calendar’s time zone).

getEra:year:month:day:fromDate:

Returns by reference the era, year, week of year, and weekday component values for a given date.

getEra:yearForWeekOfYear:weekOfYear:weekday:fromDate:

Returns by reference the era, year, week of year, and weekday component values for a given date.

getHour:minute:second:nanosecond:fromDate:

Returns by reference the hour, minute, second, and nanosecond component values for a given date.

Getting Calendar Information

calendarIdentifier

A string representing a calendar identity.

firstWeekday

The index of the first weekday of the receiver.

locale

The locale of the receiver.

maximumRangeOfUnit:

Returns the maximum range limits of the values that a given unit can take on.

minimumRangeOfUnit:

Returns the minimum range limits of the values that a given unit can take on.

minimumDaysInFirstWeek

The minimum number of days in the first week of the receiver.

ordinalityOfUnit:inUnit:forDate:

Returns, for a given absolute time, the ordinal number of a smaller calendar unit (such as a day) within a specified larger calendar unit (such as a week).

rangeOfUnit:inUnit:forDate:

Returns the range of absolute time values that a smaller calendar unit (such as a day) can take on in a larger calendar unit (such as a month) that includes a specified absolute time.

rangeOfUnit:startDate:interval:forDate:

Returns by reference the starting time and duration of a given calendar unit that contains a given date.

rangeOfWeekendStartDate:interval:containingDate:

Returns whether a given date falls within a weekend period, and if so, returns by reference the start date and time interval of the weekend range.

timeZone

The time zone for the receiver.

NSCalendarUnit

Calendrical units such as year, month, day and hour.

Scanning Dates

startOfDayForDate:

Returns the first moment of a given date as a date instance.

enumerateDatesStartingAfterDate:matchingComponents:options:usingBlock:

Computes the dates that match (or most closely match) a given set of components, and calls the block once for each of them, until the enumeration is stopped.

nextDateAfterDate:matchingComponents:options:

Returns the next date after a given date matching the given components.

nextDateAfterDate:matchingHour:minute:second:options:

Returns the next date after a given date that matches the given hour, minute, and second, component values.

nextDateAfterDate:matchingUnit:value:options:

Returns the next date after a given date matching the given calendar unit value.

NSCalendarOptions

The options for arithmetic operations involving calendars.

NSWrapCalendarComponents

A legacy constant used to control overflow in date calculations.

Calculating Dates

dateFromComponents:

Returns a date representing the absolute time calculated from given components.

dateByAddingComponents:toDate:options:

Returns a date representing the absolute time calculated by adding given components to a given date.

dateByAddingUnit:value:toDate:options:

Returns a date representing the absolute time calculated by adding the value of a given component to a given date.

dateBySettingHour:minute:second:ofDate:options:

Creates a new date calculated with the given time.

dateBySettingUnit:value:ofDate:options:

Returns a new date representing the date calculated by setting a specific component of a given date to a given value, while trying to keep lower components the same.

dateWithEra:year:month:day:hour:minute:second:nanosecond:

Returns a date created with the given components.

dateWithEra:yearForWeekOfYear:weekOfYear:weekday:hour:minute:second:nanosecond:

Returns a new date created with the given components base on a week-of-year value.

nextWeekendStartDate:interval:options:afterDate:

Returns by reference the starting date and time interval range of the next weekend period after a given date.

Comparing Dates

compareDate:toDate:toUnitGranularity:

Indicates the ordering of two given dates based on their components down to a given unit granularity.

isDate:equalToDate:toUnitGranularity:

Indicates whether two dates are equal to a given unit of granularity.

isDate:inSameDayAsDate:

Indicates whether two dates are in the same day.

isDateInToday:

Indicates whether the given date is in “today.”

isDateInTomorrow:

Indicates whether the given date is in “tomorrow.”

isDateInWeekend:

Indicates whether a given date falls within a weekend period, as defined by the calendar and the calendar's locale.

isDateInYesterday:

Indicates whether the given date is in “yesterday.”

Getting AM and PM Symbols

AMSymbol

The symbol used to represent “AM” for this calendar.

PMSymbol

The symbol used to represent “PM” for this calendar.

Getting Weekday Symbols

weekdaySymbols

A list of weekdays in this calendar.

shortWeekdaySymbols

A list of shorter-named weekdays in this calendar.

veryShortWeekdaySymbols

A list of very-shortly-named weekdays in this calendar.

standaloneWeekdaySymbols

A list of standalone weekday symbols for this calendar.

shortStandaloneWeekdaySymbols

A list of short standalone weekday symbols for this calendar.

veryShortStandaloneWeekdaySymbols

A list of very short standalone weekday symbols for this calendar.

Getting Month Symbols

monthSymbols

A list of month symbols for this calendar.

shortMonthSymbols

A list of short month symbols for this calendar.

veryShortMonthSymbols

A list of very short month symbols for this calendar.

standaloneMonthSymbols

A list of standalone month symbols for this calendar.

shortStandaloneMonthSymbols

A list of short standalone month symbols for this calendar.

veryShortStandaloneMonthSymbols

A list of very short month symbols for this calendar.

Getting Quarter Symbols

quarterSymbols

A list of quarter symbols for this calendar.

shortQuarterSymbols

A list of short quarter symbols for this calendar.

standaloneQuarterSymbols

A list of standalone quarter symbols for this calendar.

shortStandaloneQuarterSymbols

A list of short standalone quarter symbols for this calendar.

Getting Era Symbols

eraSymbols

A list of era symbols for this calendar.

longEraSymbols

A list of long era symbols for this calendar.

Recognizing Notifications

NSCalendarDayChangedNotification

A notification that is posted whenever the calendar day of the system changes, as determined by the system calendar, locale, and time zone.

Relationships

Inherits From

See Also

Calendrical Calculations

NSDateComponents

An object that specifies a date or time in terms of units (such as year, month, day, hour, and minute) to be evaluated in a calendar system and time zone.

NSTimeZone

An object containing information about standard time conventions associated with a specific geopolitical region.