Title: UIDatePicker set time does not work
Tags: ios;swift;uidatepicker;dateformatter
Question: I am trying to set the time for UIDatePicker from the text of UITextField. But it always gets reset to current time. Text on UITextField is 14:01 and this is how I am trying to set time on UIDatePicker :
let formatter = DateFormatter()
formatter.dateFormat = "HH:mm"
let time = formatter.date(from: textField.text!)
datePickerTime.date = time!
This is is the initialisation of datePickerTime :
datePickerTime = UIDatePicker(frame: CGRect(x: CGFloat(0), y: CGFloat(210), width: CGFloat(320), height: CGFloat(216)))
datePickerTime.datePickerMode = .time
datePickerTime.minimumDate = Date()
I have tried similar case with setting date(not time) and it works fine.
Comment: @Anbu.Karthik : Date picker initialisation is in viewDidLoad and I am setting datePicker time in textFieldShouldBeginEditing

Comment: @Anbu.Karthik : Sorry Anbu, it didn't work.

Comment: Guess: `let time = formatter.date(from: textField.text!)` isn't good. Check its value, my guess is that's it's not from today, but an old day, and it could explain why it doesn't change.

Comment: where u called on both methods

Comment: called in `textFieldendEditing` and `textfieldSholdReturn`

Comment: at the same time set `datePickerTime. date = Date() also

Comment: your problem is resolved or not bro



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 0, 'CommentCount': 7, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2017:00:02/14/17', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<ios><swift><uidatepicker><dateformatter>', 'Title': 'UIDatePicker set time does not work', 'ViewCount': 195, 'id': 42220374, 'user_AboutMe': '<p>With practice comes clarity</p>\n', 'user_AccountId': 259129.0, 'user_DisplayName': 'Nitish', 'user_Reputation': 13617.0, 'user_Views': 3121.0, 'user_id': 541786.0}}