1

I have a UIDatePicker, which I print the selected date as per the code below. However, it doesn't print the selected date, only the current date.

@objc func setupDiscoverButton() {

    let cell = Bundle.main.loadNibNamed("FirstTableViewCell", owner: self, options: nil)?.first as! FirstTableViewCell

    // Date
    let sessionDate = cell.datePicker.date

    print("date: \(sessionDate)")
}

This is where I make definitions for my UIDatePicker.

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = Bundle.main.loadNibNamed("FirstTableViewCell", owner: self, options: nil)?.first as! FirstTableViewCell

    // Date & time picker
    cell.datePicker.minimumDate = Date()
    cell.datePicker.timeZone = TimeZone.current

    // Discover button
    cell.discoverButton.addTarget(self, action: #selector(setupDiscoverButton), for: .touchUpInside)

    return cell
}
Annabelle Sykes
  • 121
  • 1
  • 2
  • 10

1 Answers1

0

to get change of DatePicker listen to datePickerChanged

datePicker.addTarget(self, action: #selector(datePickerChanged(picker:)), for: .valueChanged)

So your cell will be

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = Bundle.main.loadNibNamed("FirstTableViewCell", owner: self, options: nil)?.first as! FirstTableViewCell

    // Date & time picker
    cell.datePicker.minimumDate = Date()
    cell.datePicker.timeZone = TimeZone.current
cell.datePicker.addTarget(self, action: #selector(datePickerChanged(picker:)), for: .valueChanged)

    // Discover button
    cell.discoverButton.addTarget(self, action: #selector(setupDiscoverButton), for: .touchUpInside)

    return cell
}

  @objc func datePickerChanged(picker: UIDatePicker) {
        print(picker.date)
    }
Abdelahad Darwish
  • 5,969
  • 1
  • 17
  • 35
  • Perfect! thanks :) - and do you know how I change the time printed to the user's location's time zone, not the standard UTC? – Annabelle Sykes Jun 05 '18 at 22:37
  • check https://stackoverflow.com/questions/42803349/swift-3-0-convert-server-utc-time-to-local-time-and-visa-versa/42811162 about Date – Abdelahad Darwish Jun 05 '18 at 22:38
  • But I wouldn't want to use `DateFormatter` since I want to keep the date as a `Date`, not a `String` - I tried `datePicker.timeZone = TimeZone.current`, but that's not working. – Annabelle Sykes Jun 05 '18 at 22:43
  • `datePicker.locale = Locale.current` – Abdelahad Darwish Jun 05 '18 at 22:45
  • what you need from date its UTC are you need in which format? what is print result and what your target result – Abdelahad Darwish Jun 05 '18 at 22:49
  • 1
    @AnnabelleSykes You do want a DateFormatter. Keep the `Date`. But use a DateFormatter whenever you wish to show the `Date` to the user. – rmaddy Jun 05 '18 at 23:23
  • @rmaddy But would I be able to store the Date in the user's time zone? – Annabelle Sykes Jun 05 '18 at 23:28
  • 1
    @AnnabelleSykes `Date` doesn't have a timezone. Just store the date as-is. No need to do any conversion. You might find http://www.maddysoft.com/articles/dates.html helpful to better understand Date. – rmaddy Jun 05 '18 at 23:30