Two ClosedRanges for DatePicker in SwiftUI

Viewed 999

Example of date's closed range:

var dateClosedRange: ClosedRange<Date> {
    let today = Calendar.current.date(byAdding: .minute, value: -1, to: Date())!
    let seven = Calendar.current.date(byAdding: .day, value: 7, to: Date())!
    return today...seven
}

Example of time's closed range:

var timeClosedRange: ClosedRange<Date> {
    let min = Calendar.current.date(bySetting: .hour, value: 8, of: Date())!
    let max = Calendar.current.date(bySetting: .hour, value: 20, of: Date())!
    return min...max
}

Example of applying the range:

DatePicker(selection: $scheduledStart, in: timeClosedRange) {
    Text("Start Date")
}

If I put dateClosedRange then I can select a date within the specified days but time can be anywhere between all 24 hours.

If I put timeClosedRange then I can select time within 8am-8pm as per the but I can only select current date then and can not select another date.

Any way to put both the ranges in one DatePicker?

0 Answers
Related