Minimum and maximum date in UIDatePicker
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())
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