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?
Asked
Active
Viewed 135 times
1

Dani
- 3,427
- 3
- 28
- 54
-
have you try this answer? https://stackoverflow.com/a/10494471/6638533 – samAlvin Jul 07 '17 at 09:14
2 Answers
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
-
The date format string is wrong: YYYY is the "week-based" year, and DD is the day of the year. – Martin R Jul 07 '17 at 09:27