NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

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)

   

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)
    }
}

   

Very good. Thank you!

   

Hacking with Swift is sponsored by Play

SPONSORED Play is the first native iOS design tool created for designers and engineers. You can install Play for iOS and iPad today and sign up to check out the Beta of our macOS app with SwiftUI code export. We're also hiring engineers!

Click to learn more about Play!

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

Reply to this topic…

You need to create an account or log in to reply.

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.