Set Time Default in UIDatePicker

ios uidatepicker set date programmatically
customize uidatepicker swift
uidatepicker date format swift
set current date in datepicker swift
date and time picker in swift 4
swift datepicker programmatically
swift datepicker popup
uidatepicker delegate

I'm trying to populate a pickerView with a default value (String). Currently I'm able to capture it's value as a time after the user interacts with it, however I'm not able to set the time value before it becomes visible to the user.

I've tried

let test = dateFormatter.date(from: "09:00 AM")
timePicker.setDate(test, animated: false)

however that errors "Value of type '(UIDatePicker) -> ()' has no member 'setDate'"

    @IBOutlet weak var timePicker: UIDatePicker!
    @IBAction func timePicker(_ sender: UIDatePicker) {
      let dateFormatter = DateFormatter()
      dateFormatter.dateFormat = "hh:mm a"

      //my attempt at passing in a string
      let test = dateFormatter.date(from: "09:00 AM")
      timePicker.setDate(test, animated: false)

      tempTime = (dateFormatter.string(from: sender.date))//successfully captures the user's input value
}

Here's the function that launches the xib (containing the timePicker)

    func showSomeDialogue() {
       let newVC = SomeViewContoller(nibName: "SomeViewController", bundle: nil)
       let popup = PopupDialog(viewController: newVC,
                               buttonAlignment: .horizontal,
                               transitionStyle: .bounceUp,
                               tapGestureDismissal: true,
                               panGestureDismissal: false)

       present(popup, animated: true, completion: nil)
        newVC.someDelegate = self as SomeDelegate
   }

The xib's class

import UIKit

protocol MealDelegate {
  func didCreateMeal(mealTime: String)
}

class SomeViewContoller: UIViewController {

 var someDelegate: SomeDelegate!
 var tempTime = ""

 @IBOutlet weak var timePicker: UIDatePicker!
 @IBAction func timePicker(_ sender: UIDatePicker) {
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "hh:mm a"
    tempTime = (dateFormatter.string(from: sender.date))
 }

 @IBAction func someSaveBtn(_ sender: UIButton) {
    mealDelegate.didCreateMeal(mealTime: tempTime)
    self.dismiss(animated: true, completion: nil)
 }
}

Add the following to your SomeViewController class:

//MARK: - View Did Load
override func viewDidLoad() {
    super.viewDidLoad()

    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "hh:mm a"
    let date = dateFormatter.date(from: "10:57 PM")
    //or whatever date you want to display as default
    timePicker.setDate(date, animated: true)
}

UIDatePicker, You can also use the minuteInterval property to allow only specific time increments. date picker configured with the datePickerMode property set to UIDatePicker. This property allows you to override the system default with a specific locale. A UIDatePicker consists of multiple components for the date, time, seconds etc. We can specify the timeInterval between consecutive rows. Following are the four major modes of UIDatePicker: date; dateAndTime – default; time; countDownTimer; We can set the mode on the UIDatePicker either from the Interface Builder or by using the property

Add following to your view controller class:

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "hh:mm a"

let date = dateFormatter.date(from: "09:00 AM")
timePicker.date = date

setDate(_:animated:), Sets the date to display in the date picker, with an option to animate the setting. The animation rotates the wheels until the new date and time is shown under� var timeZone: Time Zone? { get set} Discussion The default value is nil , which tells the date picker to use the current time zone as returned by local ( NSTime Zone ) or the time zone used by the date picker’s calendar.

Try this,

 @IBAction func timePicker(_ sender: UITextField) {
        let datePickerView = UIDatePicker()
                datePickerView.datePickerMode = .dateAndTime

                sender.inputView = datePickerView
                datePickerView.addTarget(self, action: #selector(handleTimePicker(sender:)), for: .valueChanged)
    }
    @objc func handleTimePicker(sender: UIDatePicker) {

           let dateFormatter = DateFormatter()
           dateFormatter.dateFormat = "HH:mm "
               tempTime = dateFormatter.string(from: sender.date)
       // view.endEditing(true)
       }

1.5. Picking the Date and Time with UIDatePicker - iOS 7 , Picking the Date and Time with UIDatePicker Problem You want to allow the users and set the default countdown duration to two minutes, write code like this: true to animate the setting of the new date, otherwise false.The animation rotates the wheels until the new date and time is shown under the highlight rectangle.

iOS UIDatePicker, dateAndTime – default; time; countDownTimer. We can set the mode on the UIDatePicker either from the Interface Builder or by using the property datePickerMode� if I set the defaultDate option, it sets the field with that value during the initialization of the picker. I would like to keep the field empty if the user does not show the picker but be able to set the default date when the picker is clicked the first time. How is that possible? Thanks.

How to Set minimum and maximum date in jQuery UI Datepicker, maxDate. This option allows defining maximum possible date selection after that dates are not selectable in the widget. Its default value is null . 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.

Control date picker appearance and behavior - MATLAB, The uidatepicker function creates a date picker and sets any required properties before displaying it. empty datetime array (default) | m-by-1 datetime array. Date & Time picker View. Some times we need to pick date or time in application for this we have to create the separate UIView and add UIDatePicker then perform functionality on it. swift provide default feature in UITextField where we need to add view as UIDatepicker so when we tap on it , this will show the respective data whatever you have set on UIDatepicker mode.

Comments
  • You're trying to set the default value in the function that is called when the picker value is changed. That function is for getting and handling the user-chosen value. Call setDate() upon viewDidLoad.
  • Thank you! I spent an hour working on this problem and somehow completely missed that. The pickerView get's presented in a xib, so I'm guessing I should setDate() when that gets loaded?
  • @lajosdeme I just added an edit to show the xib function.
  • The class 'SomeViewController' must have a viewDidLoad and I guess that's the class where you wire up the date picker. Just set the default date there.
  • Step one, do not have a property and a method with the same name.
  • Terrible way to create a date.
  • I just used the code the OP provided, since his question was not about the creation of the date.
  • I do agree that I'm not using an optimal way to use dates; passing around a string is the technique that works best in my scenario.
  • I still get "Value of type '(UIDatePicker) -> ()' has no member 'setDate'"
  • As @matt suggested, never have a method and a variable with the same name. This error is pretty clear, it's trying to get setDate() of the method and not the variable.