H
H
hypzin2017-09-22 06:37:42
Swift
hypzin, 2017-09-22 06:37:42

How to set time range in swift3?

Here is the datepicker code snippet in swift3 for time:

var timePicker = UIDatePicker()
@IBOutlet weak var time: UITextField!
func createDatePicker(){
    timePicker.datePickerMode = .time
    
    let toolbar = UIToolbar()
    toolbar.sizeToFit()
    
    let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: nil, action: #selector(donePressed))
    
    time.inputAccessoryView = toolbar
    time.inputView = timePicker
}
func donePressed(){
    let timeFormatter = DateFormatter()
    timeFormatter.dateFormat = "hh:mm"
    time.text = timeFormatter.string(from: timePicker.date)
    self.view.endEditing(true)
}

In this datePicker there will be a time like 13:45
I want to set a time range such as for example 13:00-14:00 but only all this in one textfield, is it possible to implement this with a similar method? If not, how is it possible?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Globak, 2017-09-22
@hypzin

Why not see how Apple did it. For example, there is a similar functionality in the calendar and they have two pokers for this (beginning and end). Of course, you can make one custom picker to select two dates, but does it make sense? Will it be user friendly?
Any non-standard solution requires explanation. But if there is no other way, then you can make two buttons separated by a hyphen and, for each click of the button, show the date of the picker and then assign the title to the button in the form of the desired date. Then the cell will look like this
——————
Event time (start) - (end)
——————
After all operations are completed
——————
Event time ( 13:30 ) - ( 14: 00 )
——————
Buttons should be highlighted separately like the "Downloads" button in the new AppStore

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question