Instance Method
date(bySettingHour:minute:second:of:matchingPolicy:repeatedTimePolicy:direction:)
iOS 8.0+
iPadOS 8.0+
Mac Catalyst 8.0+
macOS 10.10+
tvOS 9.0+
watchOS 2.0+
Returns a new Date representing the date calculated by setting hour, minute, and second to a given time on a specified 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?
Parameters
hourA specified hour.
minuteA specified minute.
secondA specified second.
dateThe date to start calculation with.
matchingPolicySpecifies the technique the search algorithm uses to find results. Default value is
.nextTime.repeatedTimePolicySpecifies the behavior when multiple matches are found. Default value is
.first.directionSpecifies the direction in time to search. Default is
.forward.
Return Value
A Date representing the result of the search, or nil if a result could not be found.
Discussion
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.