A definition of the relationships between calendar units and absolute points in time, providing features for calculation and comparison of dates.
`Calendar` encapsulates information about systems of reckoning time in which the beginning, length, and divisions of a year are defined. It provides 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.
Members (69)
get · iOS 8.0+
eraSymbols
TypeScript
get eraSymbols(): Array<string>
Swift declaration
var eraSymbols: [String] { get }
A list of eras in this calendar, localized to the Calendar's `locale`.
For example, for English in the Gregorian calendar, returns `["BC", "AD"]`.
- note: By default, Calendars have no locale set. If you wish to receive a localized answer, be sure to set the `locale` property first - most likely to `Locale.autoupdatingCurrent`.
func ordinality(of smaller: Calendar.Component, in larger: Calendar.Component, for date: Date) -> Int?
Returns, for a given absolute time, the ordinal number of a smaller calendar component (such as a day) within a specified larger calendar component (such as a week).
The ordinality is in most cases not the same as the decomposed value of the component. Typically return values are 1 and greater. For example, the time 00:45 is in the first hour of the day, and for components `hour` and `day` respectively, the result would be 1. An exception is the week-in-month calculation, which returns 0 for days before the first week in the month containing the date.
- note: Some computations can take a relatively long time.
- parameter smaller: The smaller calendar component.
- parameter larger: The larger calendar component.
- parameter date: The absolute time for which the calculation is performed.
- returns: The ordinal number of smaller within larger at the time specified by date. Returns `nil` if larger is not logically bigger than smaller in the calendar, or the given combination of components does not make sense (or is a computation which is undefined).
Returns the first moment of a given Date, as a Date.
For example, pass in `Date()`, if you want the start of today.
If there were two midnights, it returns the first. If there was none, it returns the first moment that did exist.
- parameter date: The date to search.
- returns: The first moment of the given date.
get · iOS 8.0+
description
TypeScript
get description(): string
Swift declaration
var description: String { get }
A textual representation of this instance.
Calling this property directly is discouraged. Instead, convert an
instance of any type to a string by using the `String(describing:)`
initializer. This initializer works with any type, and uses the custom
`description` property for types that conform to
`CustomStringConvertible`:
struct Point: CustomStringConvertible {
let x: Int, y: Int
var description: String {
return "(\(x), \(y))"
}
}
let p = Point(x: 21, y: 30)
let s = String(describing: p)
print(s)
// Prints "(21, 30)"
The conversion of `p` to a string in the assignment to `s` uses the
`Point` type's `description` property.
Returns the range of the next weekend via two inout parameters. The weekend starts strictly after the given date.
If `direction` is `.backward`, then finds the previous weekend range strictly before the given date.
Note that a given entire Day within a calendar is not necessarily all in a weekend or not; weekends can start in the middle of a day in some calendars and locales.
- parameter date: The date at which to begin the search.
- parameter start: Upon return, the starting date of the next weekend if found.
- parameter interval: Upon return, the duration of the next weekend if found.
- parameter direction: Which direction in time to search. The default value is `.forward`.
- returns: `true` if the next weekend is found.
Omit the output parameters from the call. The result contains $ret and $out, with start, interval inside $out.
Returns a DateInterval of the next weekend, which starts strictly after the given date.
If `direction` is `.backward`, then finds the previous weekend range strictly before the given date.
Note that a given entire Day within a calendar is not necessarily all in a weekend or not; weekends can start in the middle of a day in some calendars and locales.
- parameter date: The date at which to begin the search.
- parameter direction: Which direction in time to search. The default value is `.forward`.
- returns: A `DateInterval`, or nil if weekends do not exist in the specific calendar or locale.
get · iOS 8.0+
customMirror
TypeScript
get customMirror(): SDKHandle
Swift declaration
var customMirror: Mirror { get }
The custom mirror for this instance.
If this type has value semantics, the mirror should be unaffected by
subsequent mutations of the instance.
func dateInterval(of component: Calendar.Component, for date: Date) -> DateInterval?
Returns the starting time and duration of a given calendar component that contains a given date.
- parameter component: A calendar component.
- parameter date: The specified date.
- returns: A new `DateInterval` if the starting time and duration of a component could be calculated, otherwise `nil`.
Returns, via two inout parameters, the starting time and duration of a given calendar component that contains a given date.
- seealso: `range(of:for:)`
- seealso: `dateInterval(of:for:)`
- parameter component: A calendar component.
- parameter start: Upon return, the starting time of the calendar component that contains the date.
- parameter interval: Upon return, the duration of the calendar component that contains the date.
- parameter date: The specified date.
- returns: `true` if the starting time and duration of a component could be calculated, otherwise `false`.
Omit the output parameters from the call. The result contains $ret and $out, with start, interval inside $out.
The maximum range limits of the values that a given component can take on.
As an example, in the Gregorian calendar the maximum range of values for the Day component is 1-31.
- parameter component: A component to calculate a range for.
- returns: The range, or nil if it could not be calculated.
Returns the minimum range limits of the values that a given component can take on.
As an example, in the Gregorian calendar the minimum range of values for the Day component is 1-28.
- parameter component: A component to calculate a range for.
- returns: The range, or nil if it could not be calculated.
get · iOS 8.0+
monthSymbols
TypeScript
get monthSymbols(): Array<string>
Swift declaration
var monthSymbols: [String] { get }
A list of months in this calendar, localized to the Calendar's `locale`.
For example, for English in the Gregorian calendar, returns `["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]`.
- note: By default, Calendars have no locale set. If you wish to receive a localized answer, be sure to set the `locale` property first - most likely to `Locale.autoupdatingCurrent`.
method · iOS 8.0+
isDateInToday
TypeScript
isDateInToday(date: globalThis.Date): boolean
Swift declaration
func isDateInToday(_ date: Date) -> Bool
Returns a Boolean value indicating whether the given date is within today, as defined by the calendar and calendar's locale.
- parameter date: The specified date.
- returns: `true` if the given date is within today.
func dateComponents(in timeZone: TimeZone, from date: Date) -> DateComponents
Returns all the date components of a date, as if in a given time zone (instead of the `Calendar` time zone).
The time zone overrides the time zone of the `Calendar` for the purposes of this calculation.
- note: If you want "date information in a given time zone" in order to display it, you should use `DateFormatter` to format the date.
- parameter timeZone: The `TimeZone` to use.
- parameter date: The `Date` to use.
- returns: All components, calculated using the `Calendar` and `TimeZone`.
func dateComponents(_ components: Set<Calendar.Component>, from start: DateComponents, to end: DateComponents) -> DateComponents
Returns the difference between two dates specified as `DateComponents`.
For components which are not specified in each `DateComponents`, but required to specify an absolute date, the base value of the component is assumed. For example, for an `DateComponents` with just a `year` and a `month` specified, a `day` of 1, and an `hour`, `minute`, `second`, and `nanosecond` of 0 are assumed.
Calendrical calculations with unspecified `year` or `year` value prior to the start of a calendar are not advised.
For each `DateComponents`, if its `timeZone` property is set, that time zone is used for it. If the `calendar` property is set, that is used rather than the receiving calendar, and if both the `calendar` and `timeZone` are set, the `timeZone` property value overrides the time zone of the `calendar` property.
- parameter components: Which components to compare.
- parameter start: The starting date components.
- parameter end: The ending date components.
- returns: The result of calculating the difference from start to end.
func dateComponents(_ components: Set<Calendar.Component>, from start: Date, to end: Date) -> DateComponents
Returns the difference between two dates.
- parameter components: Which components to compare.
- parameter start: The starting date.
- parameter end: The ending date.
- returns: The result of calculating the difference from start to end.
func dateComponents(_ components: Set<Calendar.Component>, from date: Date) -> DateComponents
Returns all the date components of a date, using the calendar time zone.
- note: If you want "date information in a given time zone" in order to display it, you should use `DateFormatter` to format the date.
- parameter date: The `Date` to use.
- returns: The date components of the specified date.
Computes the dates which match (or most closely match) a given set of components, and calls the closure once for each of them, until the enumeration is stopped.
There will be at least one intervening date which does not match all the components (or the given date itself must not match) between the given date and any result.
If `direction` is set to `.backward`, this method finds the previous match before the given date. The intent is that the same matches as for a `.forward` search will be found (that is, if you are enumerating forwards or backwards for each hour with minute "27", the seconds in the date you will get in forwards search would obviously be 00, and the same will be true in a backwards search in order to implement this rule. Similarly for DST backwards jumps which repeats times, you'll get the first match by default, where "first" is defined from the point of view of searching forwards. So, when searching backwards looking for a particular hour, with no minute and second specified, you don't get a minute and second of 59:59 for the matching hour (which would be the nominal first match within a given hour, given the other rules here, when searching backwards).
If an exact match is not possible, and requested with the `strict` option, nil is passed to the closure and the enumeration ends. (Logically, since an exact match searches indefinitely into the future, if no match is found there's no point in continuing the enumeration.)
Result dates have an integer number of seconds (as if 0 was specified for the nanoseconds property of the `DateComponents` matching parameter), unless a value was set in the nanoseconds property, in which case the result date will have that number of nanoseconds (or as close as possible with floating point numbers).
The enumeration is stopped by setting `stop` to `true` in the closure and returning. It is not necessary to set `stop` to `false` to keep the enumeration going.
- parameter start: The `Date` at which to start the search.
- parameter components: The `DateComponents` to use as input to the search algorithm.
- parameter matchingPolicy: Determines the behavior of the search algorithm when the input produces an ambiguous result.
- parameter repeatedTimePolicy: Determines the behavior of the search algorithm when the input produces a time that occurs twice on a particular day.
- parameter direction: Which direction in time to search. The default value is `.forward`, which means later in time.
- parameter block: A closure that is called with search results.
get · iOS 8.0+
longEraSymbols
TypeScript
get longEraSymbols(): Array<string>
Swift declaration
var longEraSymbols: [String] { get }
A list of longer-named eras in this calendar, localized to the Calendar's `locale`.
For example, for English in the Gregorian calendar, returns `["Before Christ", "Anno Domini"]`.
- note: By default, Calendars have no locale set. If you wish to receive a localized answer, be sure to set the `locale` property first - most likely to `Locale.autoupdatingCurrent`.
get · iOS 8.0+
quarterSymbols
TypeScript
get quarterSymbols(): Array<string>
Swift declaration
var quarterSymbols: [String] { get }
A list of quarter names in this calendar, localized to the Calendar's `locale`.
For example, for English in the Gregorian calendar, returns `["1st quarter", "2nd quarter", "3rd quarter", "4th quarter"]`.
- note: By default, Calendars have no locale set. If you wish to receive a localized answer, be sure to set the `locale` property first - most likely to `Locale.autoupdatingCurrent`.
get · iOS 8.0+
weekdaySymbols
TypeScript
get weekdaySymbols(): Array<string>
Swift declaration
var weekdaySymbols: [String] { get }
A list of weekdays in this calendar, localized to the Calendar's `locale`.
For example, for English in the Gregorian calendar, returns `["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]`.
- note: By default, Calendars have no locale set. If you wish to receive a localized answer, be sure to set the `locale` property first - most likely to `Locale.autoupdatingCurrent`.
method · iOS 8.0+
isDateInWeekend
TypeScript
isDateInWeekend(date: globalThis.Date): boolean
Swift declaration
func isDateInWeekend(_ date: Date) -> Bool
Returns a Boolean value indicating whether the given date is within a weekend period, as defined by the calendar and calendar's locale.
- parameter date: The specified date.
- returns: `true` if the given date is within a weekend.
get · iOS 8.0+
debugDescription
TypeScript
get debugDescription(): string
Swift declaration
var debugDescription: String { get }
A textual representation of this instance, suitable for debugging.
Calling this property directly is discouraged. Instead, convert an
instance of any type to a string by using the `String(reflecting:)`
initializer. This initializer works with any type, and uses the custom
`debugDescription` property for types that conform to
`CustomDebugStringConvertible`:
struct Point: CustomDebugStringConvertible {
let x: Int, y: Int
var debugDescription: String {
return "(\(x), \(y))"
}
}
let p = Point(x: 21, y: 30)
let s = String(reflecting: p)
print(s)
// Prints "(21, 30)"
The conversion of `p` to a string in the assignment to `s` uses the
`Point` type's `debugDescription` property.
method · iOS 8.0+
isDateInTomorrow
TypeScript
isDateInTomorrow(date: globalThis.Date): boolean
Swift declaration
func isDateInTomorrow(_ date: Date) -> Bool
Returns a Boolean value indicating whether the given date is within tomorrow, as defined by the calendar and calendar's locale.
- parameter date: The specified date.
- returns: `true` if the given date is within tomorrow.
method · iOS 8.0+
isDateInYesterday
TypeScript
isDateInYesterday(date: globalThis.Date): boolean
Swift declaration
func isDateInYesterday(_ date: Date) -> Bool
Returns a Boolean value indicating whether the given date is within yesterday, as defined by the calendar and calendar's locale.
- parameter date: The specified date.
- returns: `true` if the given date is within yesterday.
get · iOS 8.0+
shortMonthSymbols
TypeScript
get shortMonthSymbols(): Array<string>
Swift declaration
var shortMonthSymbols: [String] { get }
A list of shorter-named months in this calendar, localized to the Calendar's `locale`.
For example, for English in the Gregorian calendar, returns `["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]`.
- note: By default, Calendars have no locale set. If you wish to receive a localized answer, be sure to set the `locale` property first - most likely to `Locale.autoupdatingCurrent`.
get · iOS 8.0+
autoupdatingCurrent
TypeScript
static get autoupdatingCurrent(): Calendar
Swift declaration
static var autoupdatingCurrent: Calendar { get }
A calendar that tracks changes to user's preferred calendar.
If mutated, this calendar will no longer track the user's preferred calendar.
- note: The autoupdating Calendar will only compare equal to another autoupdating Calendar.
get · iOS 8.0+
shortQuarterSymbols
TypeScript
get shortQuarterSymbols(): Array<string>
Swift declaration
var shortQuarterSymbols: [String] { get }
A list of shorter-named quarters in this calendar, localized to the Calendar's `locale`.
For example, for English in the Gregorian calendar, returns `["Q1", "Q2", "Q3", "Q4"]`.
- note: By default, Calendars have no locale set. If you wish to receive a localized answer, be sure to set the `locale` property first - most likely to `Locale.autoupdatingCurrent`.
get · iOS 8.0+
shortWeekdaySymbols
TypeScript
get shortWeekdaySymbols(): Array<string>
Swift declaration
var shortWeekdaySymbols: [String] { get }
A list of shorter-named weekdays in this calendar, localized to the Calendar's `locale`.
For example, for English in the Gregorian calendar, returns `["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]`.
- note: By default, Calendars have no locale set. If you wish to receive a localized answer, be sure to set the `locale` property first - most likely to `Locale.autoupdatingCurrent`.
Find the range of the weekend around the given date, returned via two by-reference parameters.
Note that a given entire day within a calendar is not necessarily all in a weekend or not; weekends can start in the middle of a day in some calendars and locales.
- seealso: `dateIntervalOfWeekend(containing:)`
- parameter date: The date at which to start the search.
- parameter start: Upon return, the starting date of the weekend if found.
- parameter interval: Upon return, the duration of the weekend if found.
- returns: `true` if a date range could be found, and `false` if the date is not in a weekend.
Omit the output parameters from the call. The result contains $ret and $out, with start, interval inside $out.
Returns a DateInterval of the weekend contained by the given date, or nil if the date is not in a weekend.
- parameter date: The date contained in the weekend.
- returns: A `DateInterval`, or nil if the date is not in a weekend.
get · iOS 8.0+
veryShortMonthSymbols
TypeScript
get veryShortMonthSymbols(): Array<string>
Swift declaration
var veryShortMonthSymbols: [String] { get }
A list of very-shortly-named months in this calendar, localized to the Calendar's `locale`.
For example, for English in the Gregorian calendar, returns `["J", "F", "M", "A", "M", "J", "J", "A", "S", "O", "N", "D"]`.
- note: By default, Calendars have no locale set. If you wish to receive a localized answer, be sure to set the `locale` property first - most likely to `Locale.autoupdatingCurrent`.
get · iOS 8.0+
minimumDaysInFirstWeek
TypeScript
get minimumDaysInFirstWeek(): number
Swift declaration
var minimumDaysInFirstWeek: Int { get set }
The number of minimum days in the first week.
set · iOS 8.0+
minimumDaysInFirstWeek
TypeScript
set minimumDaysInFirstWeek(value: number)
Swift declaration
var minimumDaysInFirstWeek: Int { get set }
The number of minimum days in the first week.
get · iOS 8.0+
standaloneMonthSymbols
TypeScript
get standaloneMonthSymbols(): Array<string>
Swift declaration
var standaloneMonthSymbols: [String] { get }
A list of standalone months in this calendar, localized to the Calendar's `locale`.
For example, for English in the Gregorian calendar, returns `["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]`.
- note: Stand-alone properties are for use in places like calendar headers. Non-stand-alone properties are for use in context (for example, "Saturday, November 12th").
- note: By default, Calendars have no locale set. If you wish to receive a localized answer, be sure to set the `locale` property first - most likely to `Locale.autoupdatingCurrent`.
get · iOS 8.0+
veryShortWeekdaySymbols
TypeScript
get veryShortWeekdaySymbols(): Array<string>
Swift declaration
var veryShortWeekdaySymbols: [String] { get }
A list of very-shortly-named weekdays in this calendar, localized to the Calendar's `locale`.
For example, for English in the Gregorian calendar, returns `["S", "M", "T", "W", "T", "F", "S"]`.
- note: By default, Calendars have no locale set. If you wish to receive a localized answer, be sure to set the `locale` property first - most likely to `Locale.autoupdatingCurrent`.
get · iOS 8.0+
standaloneQuarterSymbols
TypeScript
get standaloneQuarterSymbols(): Array<string>
Swift declaration
var standaloneQuarterSymbols: [String] { get }
A list of standalone quarter names in this calendar, localized to the Calendar's `locale`.
For example, for English in the Gregorian calendar, returns `["1st quarter", "2nd quarter", "3rd quarter", "4th quarter"]`.
- note: Stand-alone properties are for use in places like calendar headers. Non-stand-alone properties are for use in context (for example, "Saturday, November 12th").
- note: By default, Calendars have no locale set. If you wish to receive a localized answer, be sure to set the `locale` property first - most likely to `Locale.autoupdatingCurrent`.
get · iOS 8.0+
standaloneWeekdaySymbols
TypeScript
get standaloneWeekdaySymbols(): Array<string>
Swift declaration
var standaloneWeekdaySymbols: [String] { get }
A list of standalone weekday names in this calendar, localized to the Calendar's `locale`.
For example, for English in the Gregorian calendar, returns `["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]`.
- note: Stand-alone properties are for use in places like calendar headers. Non-stand-alone properties are for use in context (for example, "Saturday, November 12th").
- note: By default, Calendars have no locale set. If you wish to receive a localized answer, be sure to set the `locale` property first - most likely to `Locale.autoupdatingCurrent`.
get · iOS 8.0+
shortStandaloneMonthSymbols
TypeScript
get shortStandaloneMonthSymbols(): Array<string>
Swift declaration
var shortStandaloneMonthSymbols: [String] { get }
A list of shorter-named standalone months in this calendar, localized to the Calendar's `locale`.
For example, for English in the Gregorian calendar, returns `["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]`.
- note: Stand-alone properties are for use in places like calendar headers. Non-stand-alone properties are for use in context (for example, "Saturday, November 12th").
- note: By default, Calendars have no locale set. If you wish to receive a localized answer, be sure to set the `locale` property first - most likely to `Locale.autoupdatingCurrent`.
get · iOS 8.0+
shortStandaloneQuarterSymbols
TypeScript
get shortStandaloneQuarterSymbols(): Array<string>
Swift declaration
var shortStandaloneQuarterSymbols: [String] { get }
A list of shorter-named standalone quarters in this calendar, localized to the Calendar's `locale`.
For example, for English in the Gregorian calendar, returns `["Q1", "Q2", "Q3", "Q4"]`.
- note: Stand-alone properties are for use in places like calendar headers. Non-stand-alone properties are for use in context (for example, "Saturday, November 12th").
- note: By default, Calendars have no locale set. If you wish to receive a localized answer, be sure to set the `locale` property first - most likely to `Locale.autoupdatingCurrent`.
get · iOS 8.0+
shortStandaloneWeekdaySymbols
TypeScript
get shortStandaloneWeekdaySymbols(): Array<string>
Swift declaration
var shortStandaloneWeekdaySymbols: [String] { get }
A list of shorter-named standalone weekdays in this calendar, localized to the Calendar's `locale`.
For example, for English in the Gregorian calendar, returns `["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]`.
- note: Stand-alone properties are for use in places like calendar headers. Non-stand-alone properties are for use in context (for example, "Saturday, November 12th").
- note: By default, Calendars have no locale set. If you wish to receive a localized answer, be sure to set the `locale` property first - most likely to `Locale.autoupdatingCurrent`.
get · iOS 8.0+
veryShortStandaloneMonthSymbols
TypeScript
get veryShortStandaloneMonthSymbols(): Array<string>
Swift declaration
var veryShortStandaloneMonthSymbols: [String] { get }
A list of very-shortly-named standalone months in this calendar, localized to the Calendar's `locale`.
For example, for English in the Gregorian calendar, returns `["J", "F", "M", "A", "M", "J", "J", "A", "S", "O", "N", "D"]`.
- note: Stand-alone properties are for use in places like calendar headers. Non-stand-alone properties are for use in context (for example, "Saturday, November 12th").
- note: By default, Calendars have no locale set. If you wish to receive a localized answer, be sure to set the `locale` property first - most likely to `Locale.autoupdatingCurrent`.
get · iOS 8.0+
veryShortStandaloneWeekdaySymbols
TypeScript
get veryShortStandaloneWeekdaySymbols(): Array<string>
Swift declaration
var veryShortStandaloneWeekdaySymbols: [String] { get }
A list of very-shortly-named weekdays in this calendar, localized to the Calendar's `locale`.
For example, for English in the Gregorian calendar, returns `["S", "M", "T", "W", "T", "F", "S"]`.
- note: Stand-alone properties are for use in places like calendar headers. Non-stand-alone properties are for use in context (for example, "Saturday, November 12th").
- note: By default, Calendars have no locale set. If you wish to receive a localized answer, be sure to set the `locale` property first - most likely to `Locale.autoupdatingCurrent`.
Returns a new Date representing the date calculated by setting hour, minute, and second to a given time on a specified Date.
If no such time exists, the next available time is returned (which could, for example, be in a different day than the nominal target date).
The intent is to return a date on the same day as the original date argument. This may result in a date which is backward than the given date, of course.
- parameter hour: A specified hour.
- parameter minute: A specified minute.
- parameter second: A specified second.
- parameter date: The date to start calculation with.
- parameter matchingPolicy: Specifies the technique the search algorithm uses to find results. Default value is `.nextTime`.
- parameter repeatedTimePolicy: Specifies the behavior when multiple matches are found. Default value is `.first`.
- parameter direction: Specifies the direction in time to search. Default is `.forward`.
- returns: A `Date` representing the result of the search, or `nil` if a result could not be found.
Returns a date created from the specified components.
- parameter components: Used as input to the search algorithm for finding a corresponding date.
- returns: A new `Date`, or nil if a date could not be found which matches the components.
Returns a new Date representing the date calculated by adding components to a given date.
- parameter components: A set of values to add to the date.
- parameter date: The starting date.
- parameter wrappingComponents: If `true`, the component should be incremented and wrap around to zero/one on overflow, and should not cause higher components to be incremented. The default value is `false`.
- returns: A new date, or nil if a date could not be calculated with the given input.
Returns a new Date representing the date calculated by adding an amount of a specific component to a given date.
- parameter component: A single component to add.
- parameter value: The value of the specified component to add.
- parameter date: The starting date.
- parameter wrappingComponents: If `true`, the component should be incremented and wrap around to zero/one on overflow, and should not cause higher components to be incremented. The default value is `false`.
- returns: A new date, or nil if a date could not be calculated with the given input.
Returns a new Date representing the date calculated by setting a specific component to a given time, and trying to keep lower components the same.
If the component already has the value, the result may be a date equal to the given date.
Changing a component's value often will require higher or coupled components to change as well. For example, setting the Weekday to Thursday usually will require the Day component to change its value, and possibly the Month and Year as well.
If no such time exists, the next available time is returned (which could, for example, be in a different day, week, month, ... than the nominal target date). Setting a component to something which would be inconsistent forces other components to change; for example, setting the Weekday to Thursday probably shifts the Day and possibly Month and Year.
The exact behavior of this method is implementation-defined. For example, if changing the weekday to Thursday, does that move forward to the next, backward to the previous, or to the nearest Thursday? The algorithm will try to produce a result which is in the next-larger component to the one given (there's a table of this mapping at the top of this document). So for the "set to Thursday" example, find the Thursday in the Week in which the given date resides (which could be a forwards or backwards move, and not necessarily the nearest Thursday). For more control over the exact behavior, use `nextDate(after:matching:matchingPolicy:behavior:direction:)`.
Determines if the date has all of the specified date components.
It may be useful to test the return value of `nextDate(after:matching:matchingPolicy:behavior:direction:)` to find out if the components were obeyed or if the method had to fudge the result value due to missing time (for example, a daylight saving time transition).
- returns: `true` if the date matches all of the components, otherwise `false`.
Computes the dates which match (or most closely match) a given set of components, returned as a `Sequence`.
If `direction` is set to `.backward`, this method finds the previous match before the start date. The intent is that the same matches as for a `.forward` search will be found. For example, if you are searching forwards or backwards for each hour with minute "27", the seconds in the date you will get in both a `.forward` and `.backward` search would be `00`. Similarly, for DST backwards jumps which repeat times, you'll get the first match by default, where "first" is defined from the point of view of searching forwards. Therefore, when searching backwards looking for a particular hour, with no minute and second specified, you don't get a minute and second of `59:59` for the matching hour but instead `00:00`.
If a range is supplied, the sequence terminates if the next result is not contained in the range. The starting point does not need to be contained in the range, but if the first result is outside of the range then the result will be an empty sequence.
If an exact match is not possible, and requested with the `strict` option, the sequence ends.
Result dates have an integer number of seconds (as if 0 was specified for the nanoseconds property of the `DateComponents` matching parameter), unless a value was set in the nanoseconds property, in which case the result date will have that number of nanoseconds, or as close as possible with floating point numbers.
- parameter start: The `Date` at which to start the search.
- parameter range: The range of dates to allow in the result. The sequence terminates if the next result is not contained in this range. If `nil`, all results are allowed.
- parameter components: The `DateComponents` to use as input to the search algorithm.
- parameter matchingPolicy: Determines the behavior of the search algorithm when the input produces an ambiguous result.
- parameter repeatedTimePolicy: Determines the behavior of the search algorithm when the input produces a time that occurs twice on a particular day.
- parameter direction: Which direction in time to search. The default value is `.forward`, which means later in time.
Returns a sequence of `Date`s, calculated by repeatedly adding an amount of `DateComponents` to a starting `Date` and then to each subsequent result.
If a range is supplied, the sequence terminates if the next result is not contained in the range. The starting point does not need to be contained in the range, but if the first result is outside of the range then the result will be an empty sequence.
- parameter start: The starting point of the search.
- parameter range: The range of dates to allow in the result. The sequence terminates if the next result is not contained in this range. If `nil`, all results are allowed.
- parameter components: The components to add or subtract.
- parameter wrappingComponents: If `true`, the component should be incremented and wrap around to zero/one on overflow, and should not cause higher components to be incremented. The default value is `false`.
- returns: A `Sequence` of `Date` values, or an empty sequence if no addition could be performed.
func dates(byAdding component: Calendar.Component, value: Int = 1, startingAt start: Date, in range: Range<Date>? = nil, wrappingComponents: Bool = false) -> some Sendable & Sequence<Date>
Returns a sequence of `Date`s, calculated by adding a scaled amount of `Calendar.Component`s to a starting `Date`.
If a range is supplied, the sequence terminates if the next result is not contained in the range. The starting point does not need to be contained in the range, but if the first result is outside of the range then the result will be an empty sequence.
- parameter start: The starting point of the search.
- parameter range: The range of dates to allow in the result. The sequence terminates if the next result is not contained in this range. If `nil`, all results are allowed.
- parameter component: A component to add or subtract.
- parameter value: The value of the specified component to add or subtract. The default value is `1`. The value can be negative, which causes subtraction.
- parameter wrappingComponents: If `true`, the component should be incremented and wrap around to zero/one on overflow, and should not cause higher components to be incremented. The default value is `false`.
- returns: A `Sequence` of `Date` values, or an empty sequence if no addition could be performed.
func range(of smaller: Calendar.Component, in larger: Calendar.Component, for date: Date) -> Range<Int>?
Returns the range of absolute time values that a smaller calendar component (such as a day) can take on in a larger calendar component (such as a month) that includes a specified absolute time.
You can use this method to calculate, for example, the range the `day` component can take on in the `month` in which `date` lies.
- parameter smaller: The smaller calendar component.
- parameter larger: The larger calendar component.
- parameter date: The absolute time for which the calculation is performed.
- returns: The range of absolute time values smaller can take on in larger at the time specified by date. Returns `nil` if larger is not logically bigger than smaller in the calendar, or the given combination of components does not make sense (or is a computation which is undefined).
Returns a Boolean value indicating whether a date is within the same day as another date, as defined by the calendar and calendar's locale.
- parameter date1: A date to check for containment.
- parameter date2: A date to check for containment.
- returns: `true` if `date1` and `date2` are in the same day.
Returns a Boolean value indicating whether two dates are equal in the given component and all larger components.
- parameter date1: A date to compare.
- parameter date2: A date to compare.
- parameter component: A granularity to compare. For example, pass `.hour` to check if two dates are in the same hour.
- returns: `true` if the given date is within tomorrow.
Compares two dates down to the specified component.
The result is `orderedSame` if they are the same in the given component and all larger components. Otherwise, the result is either `orderedAscending` or `orderedDescending`.
- parameter date1: A date to compare.
- parameter date2: A date to compare.
- parameter: component: A granularity to compare. For example, pass `.hour` to check if two dates are in the same hour.
get · iOS 8.0+
current
TypeScript
static get current(): Calendar
Swift declaration
static var current: Calendar { get }
The user's current calendar.
This calendar does not track changes that the user makes to their preferences.
get · iOS 8.0+
amSymbol
TypeScript
get amSymbol(): string
Swift declaration
var amSymbol: String { get }
The symbol used to represent "AM", localized to the Calendar's `locale`.
For example, for English in the Gregorian calendar, returns `"AM"`.
- note: By default, Calendars have no locale set. If you wish to receive a localized answer, be sure to set the `locale` property first - most likely to `Locale.autoupdatingCurrent`.
Computes the next date which matches (or most closely match) a given set of components.
The general semantics follow those of the `enumerateDates` function.
To compute a sequence of results, use the `enumerateDates` function, rather than looping and calling this method with the previous loop iteration's result.
- parameter date: The starting date.
- parameter components: The components to search for.
- parameter matchingPolicy: Specifies the technique the search algorithm uses to find results. Default value is `.nextTime`.
- parameter repeatedTimePolicy: Specifies the behavior when multiple matches are found. Default value is `.first`.
- parameter direction: Specifies the direction in time to search. Default is `.forward`.
- returns: A `Date` representing the result of the search, or `nil` if a result could not be found.
get · iOS 8.0+
pmSymbol
TypeScript
get pmSymbol(): string
Swift declaration
var pmSymbol: String { get }
The symbol used to represent "PM", localized to the Calendar's `locale`.
For example, for English in the Gregorian calendar, returns `"PM"`.
- note: By default, Calendars have no locale set. If you wish to receive a localized answer, be sure to set the `locale` property first - most likely to `Locale.autoupdatingCurrent`.
get · iOS 8.0+
timeZone
TypeScript
get timeZone(): TimeZone
Swift declaration
var timeZone: TimeZone { get set }
The time zone of the calendar.
set · iOS 8.0+
timeZone
TypeScript
set timeZone(value: TimeZone)
Swift declaration
var timeZone: TimeZone { get set }
The time zone of the calendar.
method · iOS 8.0+
component
TypeScript
component(component: Calendar_Component, date: globalThis.Date): number
Swift declaration
func component(_ component: Calendar.Component, from date: Date) -> Int
Returns the value for one component of a date.
- parameter component: The component to calculate.
- parameter date: The date to use.
- returns: The value for the component.