21

Is there any way to set a date picker to display a specific time on load? I have four date picker instances initiated from four text fields for Start Time, Finish Time, Start Date and Finish Date using the squimer/datePickerDialog subclass from GitHub that pops up in a UIAlertView.

In my app a default start time would be 7:00 AM and a default finish time would be 5:00 PM and let the user adjust around those times.

All I can see is that you can set the datePicker.defaultDate to currentDate, minimumDate or maximumDate and only once. Is it possible set the defaultDate to hardcoded strings of sTime = "07:00" and fTime = "17:00" in my calls from the ViewController?

Any help would be appreciated.

EDIT: This is how I am calling the subclass from my viewController

@IBAction func setFT(sender: UITextField) {
    resignKeyboardCompletely(sender)
    DatePickerDialog().show("Pick a Finish Time", doneButtonTitle: "Done", cancelButtonTitle: "Cancel", datePickerMode: .Time) {
        (timeFinish) -> Void in
        self.dateFormatter.dateFormat = "HH:mm"
        let finTime = self.dateFormatter.stringFromDate(timeFinish)
        self.finishTime.text = finTime
    }
    defaults.setObject(finishTime.text, forKey: "finishTime")
    print(finishTime.text)
}

Note that throughout this I am also trying to maintain persistence through NSUser Defaults.

user3118171
  • 357
  • 1
  • 2
  • 8

4 Answers4

47

Are you looking for setting the time through a string. If that''s the case you can use a date formatter like this.

let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat =  "HH:mm"

let date = dateFormatter.dateFromString("17:00")

datePicker.date = date

Adding an init to the Picker class

init(time:String) {
    super.init(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.size.width, UIScreen.mainScreen().bounds.size.height))

    setupView()
    let dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat =  "HH:mm"

    if let date = dateFormatter.dateFromString("17:00") {
        datePicker.date = date
    }
}
Moriya
  • 7,750
  • 3
  • 35
  • 53
  • That is the kind of thing I have been experimenting with but I'm not quire sure how to implement it. This is how I am suscessfully calling the subclass datePickerDialog.swift. – user3118171 Oct 29 '15 at 05:13
  • @IBAction func setFT(sender: UITextField) { resignKeyboardCompletely(sender) DatePickerDialog().show("Pick a Finish Time", doneButtonTitle: "Done", cancelButtonTitle: "Cancel", datePickerMode: .Time) { (timeFinish) -> Void in self.dateFormatter.dateFormat = "HH:mm" let finTime = self.dateFormatter.stringFromDate(timeFinish) self.finishTime.text = finTime } defaults.setObject(finishTime.text, forKey: "finishTime") print(finishTime.text) – user3118171 Oct 29 '15 at 05:15
  • put this code in viewDidLoad if you want your datePicker to start at this time... I'm not sure what you are asking though. You should probably try to explain a bit further what you are attempting and what exactly you have tried that has failed – Moriya Oct 29 '15 at 05:15
  • Sorry. Very new to all this, including stack overflow. – user3118171 Oct 29 '15 at 05:16
  • Are you using a third party data picker dialog? Or did you build DatePickerDialog yourself? – Moriya Oct 29 '15 at 05:19
  • As I said "squimer/datePickerDialog.swift subclass from GitHub" It's pretty minimalist and I have modified it slightly. – user3118171 Oct 29 '15 at 05:27
  • It seems that project doesn't give any way to modify that so you would have to modify it. easiest way would probably be to add an init(time:String) or init(date:NSDate) that then sets the internal date picker up with the code in my answer. – Moriya Oct 29 '15 at 05:37
  • Added some code to the answer for you... I haven't tested it so you might have to work around some issues but I think the general idea should be sound. – Moriya Oct 29 '15 at 05:42
4

Swift 5

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "HH:mm"
if let date = dateFormatter.date(from: "17:00") {
    print(date) // 2000-01-01 22:00:00 +0000
    datePicker.date = date
}

Additional note: If you've already set datePicker.minimumDate and you're using this dateFormat the date/time will default to it's earliest possible date--wasted an hour on this :|

BaxiaMashia
  • 115
  • 10
3

with swift 4+

let dateFormatter = DateFormatter()
dateFormatter.dateFormat =  "HH:mm"
let date = dateFormatter.date(from: "17:00")
datePicker.date = date
BaxiaMashia
  • 115
  • 10
sadat
  • 4,004
  • 2
  • 29
  • 49
0

I got here as it seemed setting the start date wasn't working. Turns out you need to set the initial date after setting the min and max dates.

FlimFlam Vir
  • 1,080
  • 9
  • 15