Is there a way to set default time using DatePicker?

Hi community,

I’m trying to set a default time in format HH:mm using a UIDatePicker (not initiated from text fields) for a startTime and endTime. I can’t find an example where this has been done. Any help will be appreciated :))

@IBOutlet weak var startPicker: UIDatePicker!
@IBOutlet weak var endPicker: UIDatePicker!

I can only see minDate and maxDate in xcode which isn’t what I need.

@IBAction func setTime(sender: UIDatePicker) {
        let formatter = DateFormatter()
        formatter.dateFormat = "HH:mm"
        
        let startPicker = formatter.date(from: "18:00")
        let endPicker = formatter.date(from: "6:00")
}

I think it’s supposed to be like this:

let startPicker = formatter.date(from: "18:00")
let endPicker = formatter.date(from: "6:00")

But then it says Initialization of immutable value 'startPicker' was never used; consider replacing with assignment to '_' or removing it

Screenshot 2022-08-15 at 15.00.20

This may help!

You set the .date property for the picker

Thanks for ur response Mikaela!

I saw this and tried it but it was still giving me errors. However, I just went ahead and did it manually here as it isn’t rlly vital but wanted to play around with it ahah :slight_smile:

Will still try this again though! Thanks

Screenshot 2022-08-15 at 15.44.55