kanwaljeet singh
kanwaljeet singh

Reputation: 59

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?

Upvotes: 3

Views: 3351

Answers (2)

mag_zbc
mag_zbc

Reputation: 6982

Use maximumDate property of your date picker.

let yesterdayDate = Calendar.current.date(byAdding: .day, value: -1, to: Date())
yourPicker.maximumDate = yesterdayDate

Upvotes: 4

Devil Decoder
Devil Decoder

Reputation: 1086

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
    }

Upvotes: 0

Related Questions