How to set maximum date in UIDatePicker?
How to set maximum date in UIDatePicker?
I am trying to create UIDatePicker
in my project which maximum date should be that of yesterday instead of current date. Is there a way to set selected date as yesterday instead of selecting current date?
UIDatePicker
Is there any reason you didn't read the documentation for
UIDatePicker
?– Ashley Mills
Jul 3 at 10:16
UIDatePicker
2 Answers
2
Use maximumDate
property of your date picker.
maximumDate
let yesterdayDate = Calendar.current.date(byAdding: .day, value: -1, to: Date())
yourPicker.maximumDate = yesterdayDate
thanks . its helpful
– kanwaljeet singh
Jul 3 at 10:11
did solve your problem?or still seeking for solution ?
– Ryan110
Jul 3 at 11:55
you can use below code to achieve this
let calender = Calendar.current
let date = Date()
let finalDate = calender.date(byAdding: Calendar.Component.day, value: -1, to: date)
if let FinalDate = finalDate{
yourPicker.maximumDate = FinalDate
}
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Possible duplicate of Minimum and maximum date in UIDatePicker
– amrdruid
Jul 3 at 10:00