MatchingPolicy
A hint to the search algorithm to control the method used for searching for dates.
- iOS
- 8.0+
- macOS
- 10.10+
- tvOS
- 9.0+
- watchOS
- 2.0+
enum MatchingPolicy
A hint to the search algorithm to control the method used for searching for dates.
enum MatchingPolicy
import FoundationEssentials
struct Calendar
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.
case nextTime
If there is no matching time before the end of the next instance of the next higher component to the highest specified component in the DateComponents
argument, the algorithm will return the next existing time which exists.
case nextTimePreservingSmallerComponents
If specified, and there is no matching time before the end of the next instance of the next higher component to the highest specified component in the DateComponents
argument, the method will return the next existing value of the missing component and preserves the lower components’ values (e.g., no 2:37am results in 3:37am, if that exists).
case previousTimePreservingSmallerComponents
If there is no matching time before the end of the next instance of the next higher component to the highest specified component in the DateComponents
argument, the algorithm will return the previous existing value of the missing component and preserves the lower components’ values.
case strict
If specified, the algorithm travels as far forward or backward as necessary looking for a match.
init(from decoder: any Decoder) throws
init(identifier: Calendar.Identifier)
Returns a new Calendar.
static var autoupdatingCurrent: Calendar { get }
A Calendar that tracks changes to user’s preferred calendar.
static var current: Calendar { get }
Returns the user’s current calendar.
var customMirror: Mirror { get }
var debugDescription: String { get }
var description: String { get }
var firstWeekday: Int { get set }
The first weekday of the calendar.
var identifier: Calendar.Identifier { get }
The identifier of the calendar.
var locale: Locale? { get set }
The locale of the calendar.
var minimumDaysInFirstWeek: Int { get set }
The number of minimum days in the first week.
var timeZone: TimeZone { get set }
The time zone of the calendar.
static func == (lhs: Calendar, rhs: Calendar) -> Bool
func compare(_ date1: Date, to date2: Date, toGranularity component: Calendar.Component) -> ComparisonResult
Compares the given dates down to the given component, reporting them orderedSame
if they are the same in the given component and all larger components, otherwise either orderedAscending
or orderedDescending
.
func component(_ component: Calendar.Component, from date: Date) -> Int
Returns the value for one component of a date.
func date(_ date: Date, matchesComponents components: DateComponents) -> Bool
Determine if the Date
has all of the specified DateComponents
.
func date(byAdding components: DateComponents, to date: Date, wrappingComponents: Bool = false) -> Date?
Returns a new Date
representing the date calculated by adding components to a given date.
func date(byAdding component: Calendar.Component, value: Int, to date: Date, wrappingComponents: Bool = false) -> Date?
Returns a new Date
representing the date calculated by adding an amount of a specific component to a given date.
func date(bySetting component: Calendar.Component, value: Int, of date: 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. If the component already has that value, this may result in a date which is the same as the given date.
func date(bySettingHour hour: Int, minute: Int, second: Int, of date: Date, matchingPolicy: Calendar.MatchingPolicy = .nextTime, repeatedTimePolicy: Calendar.RepeatedTimePolicy = .first, direction: Calendar.SearchDirection = .forward) -> 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 components: DateComponents) -> Date?
Returns a date created from the specified components.
func dateComponents(_ components: Set<Calendar.Component>, from date: Date) -> DateComponents
Returns all the date components of a date, using the calendar time zone.
func dateComponents(_ components: Set<Calendar.Component>, from start: DateComponents, to end: DateComponents) -> DateComponents
Returns the difference between two dates specified as DateComponents
.
func dateComponents(_ components: Set<Calendar.Component>, from start: Date, to end: Date) -> DateComponents
Returns the difference between two dates.
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).
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.
func dateInterval(of component: Calendar.Component, start: inout Date, interval: inout TimeInterval, for date: Date) -> Bool
Returns, via two inout parameters, the starting time and duration of a given calendar component that contains a given date.
func dateIntervalOfWeekend(containing date: 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: Date, start: inout Date, interval: inout TimeInterval) -> Bool
Finds the range of the weekend around the given date, and returns the starting date and duration of the weekend via two inout parameters.
func dates(byAdding components: DateComponents, startingAt start: Date, in range: Range<Date>? = nil, wrappingComponents: Bool = false) -> some Sendable & Sequence<Date>
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.
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.
func dates(byMatching components: DateComponents, startingAt start: Date, in range: Range<Date>? = nil, matchingPolicy: Calendar.MatchingPolicy = .nextTime, repeatedTimePolicy: Calendar.RepeatedTimePolicy = .first, direction: Calendar.SearchDirection = .forward) -> some Sendable & Sequence<Date>
Computes the dates which match (or most closely match) a given set of components, returned as a Sequence
.
func encode(to encoder: any Encoder) throws
func enumerateDates(startingAfter start: Date, matching components: DateComponents, matchingPolicy: Calendar.MatchingPolicy, repeatedTimePolicy: Calendar.RepeatedTimePolicy = .first, direction: Calendar.SearchDirection = .forward, using block: (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 hasher: inout Hasher)
func isDate(_ date1: Date, equalTo date2: Date, toGranularity component: Calendar.Component) -> Bool
Compares the given dates down to the given component, reporting them equal if they are the same in the given component and all larger components.
func isDate(_ date1: Date, inSameDayAs date2: Date) -> Bool
Returns true
if the given date is within the same day as another date, as defined by the calendar and calendar’s locale.
func isDateInToday(_ date: Date) -> Bool
Returns true
if the given date is within today, as defined by the calendar and calendar’s locale.
func isDateInTomorrow(_ date: Date) -> Bool
Returns true
if the given date is within tomorrow, as defined by the calendar and calendar’s locale.
func isDateInWeekend(_ date: Date) -> Bool
Returns true
if the given date is within a weekend period, as defined by the calendar and calendar’s locale.
func isDateInYesterday(_ date: Date) -> Bool
Returns true
if the given date is within yesterday, as defined by the calendar and calendar’s locale.
func maximumRange(of component: Calendar.Component) -> Range<Int>?
The maximum range limits of the values that a given component can take on in the receive
func minimumRange(of component: Calendar.Component) -> Range<Int>?
Returns the minimum range limits of the values that a given component can take on in the receiver.
func nextDate(after date: Date, matching components: DateComponents, matchingPolicy: Calendar.MatchingPolicy, repeatedTimePolicy: Calendar.RepeatedTimePolicy = .first, direction: Calendar.SearchDirection = .forward) -> Date?
Computes the next date which matches (or most closely matches) a given set of components.
func nextWeekend(startingAfter date: Date, direction: Calendar.SearchDirection = .forward) -> DateInterval?
Returns a DateInterval
of the next weekend, which starts strictly after the given date.
func nextWeekend(startingAfter date: Date, start: inout Date, interval: inout TimeInterval, direction: Calendar.SearchDirection = .forward) -> Bool
Returns the range of the next weekend via two inout parameters. The weekend starts strictly after the given date.
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).
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.
func startOfDay(for date: Date) -> Date
Returns the first moment of a given Date, as a Date.
enum Component
An enumeration for the various components of a calendar date.
struct ComponentSet
enum Identifier
Calendar supports many different kinds of calendars. Each is identified by an identifier here.
struct RecurrenceRule
A rule which specifies how often an event should repeat in the future
enum RepeatedTimePolicy
Determines 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 SearchDirection
The direction in time to search.
protocol Copyable
A type whose values can be implicitly or explicitly copied.
protocol Decodable
A type that can decode itself from an external representation.
protocol Encodable
A type that can encode itself to an external representation.
protocol Equatable
A type that can be compared for value equality.
protocol Escapable
protocol Hashable : Equatable
A type that can be hashed into a Hasher
to produce an integer hash value.
protocol Sendable
init(from decoder: any Decoder) throws
func encode(to encoder: any Encoder) throws
static func != (lhs: Self, rhs: Self) -> Bool
Returns a Boolean value indicating whether two values are not equal.