I have making and order app I have take an outlet
named: selectedDate
and I want to retrieve the selected date to a variable. What should I place in View did load.
@IBOutlet weak var selectedDate: UIDatePicker!
func datePickerChanged(selectedDate:UIDatePicker) {
var dateFormatter = DateFormatter()
dateFormatter.dateStyle = DateFormatter.Style.FullStyle
dateFormatter.timeStyle = DateFormatter.Style.FulltStyle
var strDate = dateFormatter.string(from: selectedDate.date)
}
class ViewController: UIViewController {
@IBOutlet weak var datePicker: UIDatePicker!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func datePickerChanged(sender: UIDatePicker) {
print("print \(sender.date)")
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MMM dd, YYYY"
let somedateString = dateFormatter.stringFromDate(sender.date)
print(somedateString) // "somedateString" is your string date
}
}
You have to create a IBAction for datePicker and name it as “datePickerChanged” , just like you create outlet of object. Choose connection type "Action" when you are dragging object to Viewcontroller.