Contents

NSCalendar

A definition of the relationships between calendar units and absolute points in time, providing features for calculation and comparison of dates.

Declaration

class NSCalendar

Overview

In Swift, this object bridges to Calendar; use NSCalendar when you need reference semantics or other Foundation-specific behavior.

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, CFCalendar. 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 (gregorian), but there remain exceptions to this trend. For example:

  • In Saudi Arabia, some locales use primarily the Islamic Umm al-Qura calendar (islamicUmmAlQura).

  • In Ethiopia, some locales use primarily the Ethiopian calendar (ethiopicAmeteMihret or ethiopicAmeteAlem).

  • In Iran and Afghanistan, some locales use primarily the Persian calendar (persian).

  • In Thailand, some locales use primarily the Buddhist calendar (buddhist).

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

  • India also uses the Indian national calendar (indian).

  • Israel also uses the Hebrew calendar (hebrew).

  • China mainland and other regions also use the Chinese calendar (chinese), primarily to calculate astronomical date and Chinese traditional holidays.

  • Japan also uses the Japanese calendar (japanese), primarily to add year names.

Independent of any particular locale, certain calendars are used primarily to calculate dates for religious observances. Among these are the Buddhist (buddhist), Coptic (coptic), Hebrew (hebrew), and Islamic (islamic) 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 regions continued to use the Julian calendar, with Turkey being the last country or region to adopt the Gregorian calendar, in 1926. As a result of the staggered adoption, the transition period for regions 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, date(from:), date(byAdding:to: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(_:from:) and components(_:from:to:options:)) take a bit mask parameter that specifies which components to calculate when returning an NSDateComponents object. The bit mask is composed of NSCalendar.Unit 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

Getting the User’s Calendar

Extracting Components

Getting Calendar Information

Scanning Dates

Calculating Dates

Comparing Dates

Getting AM and PM Symbols

Getting Weekday Symbols

Getting Month Symbols

Getting Quarter Symbols

Getting Era Symbols

Recognizing Notifications

Initializers