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      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

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.