Structure
Calendar
iOS 8.0+
iPadOS 8.0+
Mac Catalyst 8.0+
macOS 10.10+
tvOS 9.0+
watchOS 2.0+
A definition of the relationships between calendar units and absolute points in time, providing features for calculation and comparison of dates.
struct Calendar
Overview
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.
Topics
Structures
struct RecurrenceRuleA rule which specifies how often an event should repeat in the future
Operators
Initializers
init(identifier: Calendar.Identifier)Returns a new Calendar.
Instance Properties
var amSymbol: StringThe symbol used to represent “AM”, localized to the Calendar’slocale.var eraSymbols: [String]A list of eras in this calendar, localized to the Calendar’slocale.var firstWeekday: IntThe first day of the week for the calendar.var identifier: Calendar.IdentifierThe identifier of the calendar.var locale: Locale?The locale of the calendar.var longEraSymbols: [String]A list of longer-named eras in this calendar, localized to the Calendar’slocale.var minimumDaysInFirstWeek: IntThe number of minimum days in the first week.var monthSymbols: [String]A list of months in this calendar, localized to the Calendar’slocale.var pmSymbol: StringThe symbol used to represent “PM”, localized to the Calendar’slocale.var quarterSymbols: [String]A list of quarter names in this calendar, localized to the Calendar’slocale.var shortMonthSymbols: [String]A list of shorter-named months in this calendar, localized to the Calendar’slocale.var shortQuarterSymbols: [String]A list of shorter-named quarters in this calendar, localized to the Calendar’slocale.var shortStandaloneMonthSymbols: [String]A list of shorter-named standalone months in this calendar, localized to the Calendar’slocale.var shortStandaloneQuarterSymbols: [String]A list of shorter-named standalone quarters in this calendar, localized to the Calendar’slocale.var shortStandaloneWeekdaySymbols: [String]A list of shorter-named standalone weekdays in this calendar, localized to the Calendar’slocale.var shortWeekdaySymbols: [String]A list of shorter-named weekdays in this calendar, localized to the Calendar’slocale.var standaloneMonthSymbols: [String]A list of standalone months in this calendar, localized to the Calendar’slocale.var standaloneQuarterSymbols: [String]A list of standalone quarter names in this calendar, localized to the Calendar’slocale.var standaloneWeekdaySymbols: [String]A list of standalone weekday names in this calendar, localized to the Calendar’slocale.var timeZone: TimeZoneThe time zone of the calendar.var veryShortMonthSymbols: [String]A list of very-shortly-named months in this calendar, localized to the Calendar’slocale.var veryShortStandaloneMonthSymbols: [String]A list of very-shortly-named standalone months in this calendar, localized to the Calendar’slocale.var veryShortStandaloneWeekdaySymbols: [String]A list of very-shortly-named weekdays in this calendar, localized to the Calendar’slocale.var veryShortWeekdaySymbols: [String]A list of very-shortly-named weekdays in this calendar, localized to the Calendar’slocale.var weekdaySymbols: [String]A list of weekdays in this calendar, localized to the Calendar’slocale.
Instance Methods
func compare(Date, to: Date, toGranularity: Calendar.Component) -> ComparisonResultCompares two dates down to the specified component.func component(Calendar.Component, from: Date) -> IntReturns the value for one component of a date.func date(Date, matchesComponents: DateComponents) -> BoolDetermines if the date has all of the specified date components.func date(byAdding: DateComponents, to: Date, wrappingComponents: Bool) -> Date?Returns a new Date representing the date calculated by adding components to a given date.func date(byAdding: Calendar.Component, value: Int, to: Date, wrappingComponents: Bool) -> Date?Returns a new Date representing the date calculated by adding an amount of a specific component to a given date.func date(bySetting: Calendar.Component, value: Int, of: Date) -> Date?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.func date(bySettingHour: Int, minute: Int, second: Int, of: Date, matchingPolicy: Calendar.MatchingPolicy, repeatedTimePolicy: Calendar.RepeatedTimePolicy, direction: Calendar.SearchDirection) -> Date?Returns a new Date representing the date calculated by setting hour, minute, and second to a given time on a specified Date.func date(from: DateComponents) -> Date?Returns a date created from the specified components.func dateComponents(Set<Calendar.Component>, from: Date) -> DateComponentsReturns all the date components of a date, using the calendar time zone.func dateComponents(_:from:to:)Returns the difference between two dates.func dateComponents(in: TimeZone, from: Date) -> DateComponentsReturns all the date components of a date, as if in a given time zone (instead of theCalendartime zone).func dateInterval(of: Calendar.Component, for: Date) -> DateInterval?Returns the starting time and duration of a given calendar component that contains a given date.func dateInterval(of: Calendar.Component, start: inout Date, interval: inout TimeInterval, for: Date) -> BoolReturns, via two inout parameters, the starting time and duration of a given calendar component that contains a given date.func dateIntervalOfWeekend(containing: Date) -> DateInterval?Returns a DateInterval of the weekend contained by the given date, or nil if the date is not in a weekend.func dateIntervalOfWeekend(containing: Date, start: inout Date, interval: inout TimeInterval) -> BoolFind the range of the weekend around the given date, returned via two by-reference parameters.func dates(byAdding: DateComponents, startingAt: Date, in: Range<Date>?, wrappingComponents: Bool) -> some Sendable & Sequence<Date>Returns a sequence ofDates, calculated by repeatedly adding an amount ofDateComponentsto a startingDateand 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.func dates(byAdding: Calendar.Component, value: Int, startingAt: Date, in: Range<Date>?, wrappingComponents: Bool) -> some Sendable & Sequence<Date>Returns a sequence ofDates, calculated by adding a scaled amount ofCalendar.Components to a startingDate. 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.func dates(byMatching: DateComponents, startingAt: Date, in: Range<Date>?, matchingPolicy: Calendar.MatchingPolicy, repeatedTimePolicy: Calendar.RepeatedTimePolicy, direction: Calendar.SearchDirection) -> some Sendable & Sequence<Date>Computes the dates which match (or most closely match) a given set of components, returned as aSequence.func enumerateDates(startingAfter: Date, matching: DateComponents, matchingPolicy: Calendar.MatchingPolicy, repeatedTimePolicy: Calendar.RepeatedTimePolicy, direction: Calendar.SearchDirection, using: (Date?, Bool, inout Bool) -> Void)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.func hash(into: inout Hasher)func isDate(Date, equalTo: Date, toGranularity: Calendar.Component) -> BoolReturns a Boolean value indicating whether two dates are equal in the given component and all larger components.func isDate(Date, inSameDayAs: Date) -> BoolReturns a Boolean value indicating whether a date is within the same day as another date, as defined by the calendar and calendar’s locale.func isDateInToday(Date) -> BoolReturns a Boolean value indicating whether the given date is within today, as defined by the calendar and calendar’s locale.func isDateInTomorrow(Date) -> BoolReturns a Boolean value indicating whether the given date is within tomorrow, as defined by the calendar and calendar’s locale.func isDateInWeekend(Date) -> BoolReturns a Boolean value indicating whether the given date is within a weekend period, as defined by the calendar and calendar’s locale.func isDateInYesterday(Date) -> BoolReturns a Boolean value indicating whether the given date is within yesterday, as defined by the calendar and calendar’s locale.func maximumRange(of: Calendar.Component) -> Range<Int>?The maximum range limits of the values that a given component can take on.func minimumRange(of: Calendar.Component) -> Range<Int>?Returns the minimum range limits of the values that a given component can take on.func nextDate(after: Date, matching: DateComponents, matchingPolicy: Calendar.MatchingPolicy, repeatedTimePolicy: Calendar.RepeatedTimePolicy, direction: Calendar.SearchDirection) -> Date?Computes the next date which matches (or most closely match) a given set of components.func nextWeekend(startingAfter: Date, direction: Calendar.SearchDirection) -> DateInterval?Returns a DateInterval of the next weekend, which starts strictly after the given date.func nextWeekend(startingAfter: Date, start: inout Date, interval: inout TimeInterval, direction: Calendar.SearchDirection) -> BoolReturns the range of the next weekend via two inout parameters. The weekend starts strictly after the given date.func ordinality(of: Calendar.Component, in: Calendar.Component, for: 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).func range(of: Calendar.Component, in: Calendar.Component, for: 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.func startOfDay(for: Date) -> DateReturns the first moment of a given Date, as a Date.
Type Properties
static var autoupdatingCurrent: CalendarA calendar that tracks changes to user’s preferred calendar.static var current: CalendarThe user’s current calendar.
Enumerations
enum ComponentAn enumeration for the various components of a calendar date.enum IdentifierAn enumeration for the available calendars.enum MatchingPolicyA hint to the search algorithm to control the method used for searching for dates.enum RepeatedTimePolicyDetermines which result to use when a time is repeated on a day in a calendar (for example, during a daylight saving transition when the times between 2:00am and 3:00am may happen twice).enum SearchDirectionThe direction in time to search.
Default Implementations
Relationships
Conforms To
Foundation.ReferenceConvertibleSwift.CopyableSwift.CustomDebugStringConvertibleSwift.CustomReflectableSwift.CustomStringConvertibleSwift.DecodableSwift.EncodableSwift.EquatableSwift.EscapableSwift.HashableSwift.SendableSwift.SendableMetatype