Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

showing datepicker 18 years back and lock the ui datepicker in ios

Tags:

xcode

ios

iphone

In my project i need to show the date picker 18 back and i need to lock the 80 years backs so how can i show those date in datepicker can any one help me to find here here am adding code my code i printed in log but i need to display on my datepicker so how can i display

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: -100];
NSDate * maxDate = [gregorian dateByAddingComponents: comps toDate: currentDate options: 0];
[comps release];

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

NSLog(@"minDate   %@", minDate);
NSLog(@"maxDate%@", maxDate);
like image 781
sabir Avatar asked Oct 12 '12 06:10

sabir


1 Answers

Hi first of all minimum date must be 100 year back date and max date must be 18 year back date and then set pickerView's date to a date between range of dates so your final code will be looks like

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

self.datePickerView.minimumDate = minDate;
self.datePickerView.maximumDate = maxDate;
self.datePickerView.date = minDate;

Hopefully this will be helpful for you.

like image 169
abhishekkharwar Avatar answered Sep 23 '22 06:09

abhishekkharwar