Skip to content

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

hour

A specified hour.

minute

A specified minute.

second

A specified second.

date

The date to start calculation with.

matchingPolicy

Specifies the technique the search algorithm uses to find results. Default value is .nextTime.

repeatedTimePolicy

Specifies the behavior when multiple matches are found. Default value is .first.

direction

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