1

In Swift, I have a var datePicker = UIDatePicker(). I'm trying to set the datePicker to have a minimum value of Jan, 1st, 1920 (I want a range from Jan, 1st, 1920 to current date. I read how to set the maximumDate of the UIDatePicker, that was easy, but I couldn't find how to set the minimum to a specific date. I read that it should be datePicker.minimumDate but I'm not sure how to set the exact date from above. Suggestions?

Dani
  • 3,427
  • 3
  • 28
  • 54

2 Answers2

3
  • Create appropriate date components

    let components = DateComponents(year: 1920, month: 1, day: 1)
    
  • Create a date from the components

    let minimumDate = Calendar.current.date(from: components)
    
  • Set the minimum date

    let picker = UIDatePicker()
    picker.minimumDate = minimumDate
    
vadian
  • 274,689
  • 30
  • 353
  • 361
  • Yeah, that works! Thanks. I'll accept your answer in a bit (apparently I cant in the next 8 min) – Dani Jul 07 '17 at 09:19
0

You can use the NSDateFormatter as the following:

let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "YYYY-MM-DD"
let date = dateFormatter.dateFromString("1920-01-01")

let datePicker = UIDatePicker()
datePicker.minimumDate = date
JMIT
  • 169
  • 2
  • 15