Class

NSCalendar

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.

Overview

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).

  • And 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 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 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 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 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.

Nested Types

NSCalendarIdentifier

The type of a calendar identifier. See Calendar Identifiers for defined values.

NSCalendarOptions

The options for arithmetic operations involving calendars.

NSCalendarUnit

Specify calendrical units such as day and month.

Symbols

Creating and Initializing Calendars

init?(identifier: NSCalendar.Identifier)

Creates and returns a new NSCalendar object specified by a given identifier.

init?(calendarIdentifier: NSCalendar.Identifier)

Initializes a newly-allocated NSCalendar object for the calendar specified by a given identifier.

Getting Information About a Calendar

var calendarIdentifier: NSCalendar.Identifier

A string representing a calendar identity.

var firstWeekday: Int

The index of the first weekday of the receiver.

var locale: Locale?

The locale of the receiver.

func maximumRange(of: NSCalendar.Unit)

The maximum range limits of the values that a given unit can take on in the receive

var minimumDaysInFirstWeek: Int

The minimum number of days, an integer value, in the first week of the receiver.

func minimumRange(of: NSCalendar.Unit)

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

func ordinality(of: NSCalendar.Unit, in: NSCalendar.Unit, for: Date)

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).

func range(of: NSCalendar.Unit, in: NSCalendar.Unit, for: Date)

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.

func range(of: NSCalendar.Unit, start: AutoreleasingUnsafeMutablePointer<NSDate?>?, interval: UnsafeMutablePointer<TimeInterval>?, for: Date)

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

func range(ofWeekendStart: AutoreleasingUnsafeMutablePointer<NSDate?>?, interval: UnsafeMutablePointer<TimeInterval>?, containing: Date)

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.

var timeZone: TimeZone

The time zone for the receiver.

Calendrical Calculations

func date(byAdding: DateComponents, to: Date, options: NSCalendar.Options = [])

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

func date(byAdding: NSCalendar.Unit, value: Int, to: Date, options: NSCalendar.Options = [])

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

func date(from: DateComponents)

Returns a new NSDate object representing the absolute time calculated from given components.

func enumerateDates(startingAfter: Date, matching: DateComponents, options: NSCalendar.Options = [], using: (Date?, Bool, UnsafeMutablePointer<ObjCBool>) -> Void)

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.

func date(bySettingUnit: NSCalendar.Unit, value: Int, of: Date, options: NSCalendar.Options = [])

Returns a new NSDate object 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.

func date(era: Int, yearForWeekOfYear: Int, weekOfYear: Int, weekday: Int, hour: Int, minute: Int, second: Int, nanosecond: Int)

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

func date(Date, matchesComponents: DateComponents)

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

func nextDate(after: Date, matching: DateComponents, options: NSCalendar.Options = [])

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

func nextDate(after: Date, matchingHour: Int, minute: Int, second: Int, options: NSCalendar.Options = [])

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

func nextDate(after: Date, matching: NSCalendar.Unit, value: Int, options: NSCalendar.Options = [])

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

func nextWeekendStart(AutoreleasingUnsafeMutablePointer<NSDate?>?, interval: UnsafeMutablePointer<TimeInterval>?, options: NSCalendar.Options = [], after: Date)

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

func startOfDay(for: Date)

Returns the first moment date of a given date

Comparing Dates

func compare(Date, to: Date, toUnitGranularity: NSCalendar.Unit)

Returns an NSComparisonResult value that indicates the ordering of two given dates based on their components down to a given unit granularity.

func isDate(Date, equalTo: Date, toUnitGranularity: NSCalendar.Unit)

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

func isDate(Date, inSameDayAs: Date)

Returns whether two dates are in the same day.

func isDateInToday(Date)

Returns whether the given date is in “today.”

func isDateInTomorrow(Date)

Returns whether the given date is in “tomorrow.”

func isDateInWeekend(Date)

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

func isDateInYesterday(Date)

Returns whether the given date is in “yesterday.”

Extracting Components

func component(NSCalendar.Unit, from: Date)

Returns the specified date component from a given date.

func components(NSCalendar.Unit, from: Date)

Returns a NSDateComponents object containing a given date decomposed into specified components.

func components(NSCalendar.Unit, from: Date, to: Date, options: NSCalendar.Options = [])

Returns, as an NSDateComponents object using specified components, the difference between two supplied dates.

func components(NSCalendar.Unit, from: DateComponents, to: DateComponents, options: NSCalendar.Options = [])

Returns an NSDateComponents object representing the difference between start and end NSDate objects constructed from given NSDateComponents objects.

func components(in: TimeZone, from: Date)

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

AM and PM Symbols

var amSymbol: String

The AM symbol for the receiver.

var pmSymbol: String

The PM symbol for the receiver.

Weekday Symbols

var weekdaySymbols: [String]

An array of weekday symbols for the receiver.

var shortWeekdaySymbols: [String]

An array of short weekday symbols for the receiver.

var veryShortWeekdaySymbols: [String]

An array of very short weekday symbols for the receiver.

var standaloneWeekdaySymbols: [String]

An array of standalone weekday symbols for the receiver.

var shortStandaloneWeekdaySymbols: [String]

An array of short standalone weekday symbols for the receiver.

var veryShortStandaloneWeekdaySymbols: [String]

An array of very short standalone weekday symbols for the receiver.

Month Symbols

var monthSymbols: [String]

An array of month symbols for the receiver.

var shortMonthSymbols: [String]

An array of short month symbols for the receiver.

var veryShortMonthSymbols: [String]

An array of very short month symbols for the receiver.

var standaloneMonthSymbols: [String]

An array of standalone month symbols for the receiver.

var shortStandaloneMonthSymbols: [String]

An array of short standalone month symbols for the receiver.

var veryShortStandaloneMonthSymbols: [String]

An array of very short month symbols for the receiver.

Quarter Symbols

var quarterSymbols: [String]

An array of quarter symbols for the receiver.

var shortQuarterSymbols: [String]

An array of short quarter symbols for the receiver.

var standaloneQuarterSymbols: [String]

An array of standalone quarter symbols for the receiver.

var shortStandaloneQuarterSymbols: [String]

An array of short standalone quarter symbols for the receiver.

Era Symbols

var eraSymbols: [String]

An array of era symbols for the receiver.

var longEraSymbols: [String]

An array of long era symbols for the receiver

Constants

Identifier

The type of a calendar identifier. See Calendar Identifiers for defined values.

Calendar Identifiers

Specify the identifier of the calendar, such as Gregorian, which is the common calendar in Europe, the Western Hemisphere, and elsewhere.

Notifications

Notifications are posted through [NSNotificationCenter defaultCenter] when the system day changes.

Unit

Specify calendrical units such as day and month.

Options

The options for arithmetic operations involving calendars.

Relationships

Inherits From