Minimum and maximum date in UIDatePicker

Solution 1:

Try this:

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *currentDate = [NSDate date];
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setYear:-18];
NSDate *minDate = [gregorian dateByAddingComponents:comps toDate:currentDate  options:0];
[comps setYear:-150];
NSDate *maxDate = [gregorian dateByAddingComponents:comps toDate:currentDate  options:0];
[comps release];

self.datePicker.minimumDate = minDate;
self.datePicker.maximumDate = maxDate;

It may be easily translated to Swift 4.2:

    let calendar = Calendar(identifier: .gregorian)

    let currentDate = Date()
    var components = DateComponents()
    components.calendar = calendar

    components.year = -18
    components.month = 12
    let maxDate = calendar.date(byAdding: components, to: currentDate)!

    components.year = -150
    let minDate = calendar.date(byAdding: components, to: currentDate)!

    picker.minimumDate = minDate
    picker.maximumDate = maxDate

Solution 2:

Look at this for Swift version. User negative value (-) to subtract and positive value to add date component.

1. Set minimum date

 yourDatePicker.minimumDate = Calendar.current.date(byAdding: .year, value: -1, to: Date())

2. Set maximum date

 yourDatePicker.maximumDate = Calendar.current.date(byAdding: .year, value: 1, to: Date())

Solution 3:

Swift 5.1

extension UIDatePicker {
    func set18YearValidation() {
        let currentDate: Date = Date()
        var calendar: Calendar = Calendar(identifier: Calendar.Identifier.gregorian)
        calendar.timeZone = TimeZone(identifier: "UTC")!
        var components: DateComponents = DateComponents()
        components.calendar = calendar
        components.year = -18
        let maxDate: Date = calendar.date(byAdding: components, to: currentDate)!
        components.year = -150
        let minDate: Date = calendar.date(byAdding: components, to: currentDate)!
        self.minimumDate = minDate
        self.maximumDate = maxDate
    } }

Solution 4:

This is how it would look like in 2017 (Swift 3)

    var components = DateComponents()
    components.year = -100
    let minDate = Calendar.current.date(byAdding: components, to: Date())

    components.year = -18
    let maxDate = Calendar.current.date(byAdding: components, to: Date())

    datePicker.minimumDate = minDate
    datePicker.maximumDate = maxDate

Solution 5:

In Swift 2.1:

    let currentDate: NSDate = NSDate()

    let calendar: NSCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
    // let calendar: NSCalendar = NSCalendar.currentCalendar()
    calendar.timeZone = NSTimeZone(name: "UTC")!

    let components: NSDateComponents = NSDateComponents()
    components.calendar = calendar

    components.year = -18
    let minDate: NSDate = calendar.dateByAddingComponents(components, toDate: currentDate, options: NSCalendarOptions(rawValue: 0))!

    components.year = -150
    let maxDate: NSDate = calendar.dateByAddingComponents(components, toDate: currentDate, options: NSCalendarOptions(rawValue: 0))!

    self.dateOfBirthUIDatePicker.minimumDate = minDate
    self.dateOfBirthUIDatePicker.maximumDate = maxDate

    print("minDate: \(minDate)")
    print("maxDate: \(maxDate)")