UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Date Picker

Forums > 100 Days of SwiftUI

In Day 26 lesson, Paul writes a function to create a date range...

func exampleDates() {
    // create a second Date instance set to one day in seconds from now
    let tomorrow = Date.now.addingTimeInterval(86400)

    // create a range from those two
    let range = Date.now...tomorrow
}

How would I use that for the :in parameter when creating a DatePicker? That is, how would I use the range constant as the in: parameter?

DatePicker("Please enter date", selection: $wakeUp, in: ????, displayedComponents: .hourAndMinute)

2      

struct ContentView: View {

    func exampleDates() -> ClosedRange<Date> {
        // create a second Date instance set to one day in seconds from now
        let tomorrow = Date.now.addingTimeInterval(86400)

        // create a range from those two
        let range = Date.now...tomorrow

        return range
    }

    @State private var wakeUp = Date()

    var body: some View {
        DatePicker("Please enter date", selection: $wakeUp, in: exampleDates(), displayedComponents: .hourAndMinute)
    }
}

2      

Very good. Thank you!

2      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.