Set Minimum date to UIDatePicker
I have a date in开发者_Python百科 UILabel(like 'Dec 14, 2010). This date i want to set as minimum date of my UIDatepicker. How can i do this please reply?
Thanks Nishant
Convert your UILabel text in to date format using NSDateFormatter
and put that value replacing "Date" in following example:
UIDatepicker *datePicker;
NSDate *Date=[NSDate date];
datePicker.minimumDate=Date;
You can use NSDateFormatter
and use setDateFormat
method for setting Date format.
[formatter setDateFormat:@"MMM dd,yyyy"];
NSDate *date = [formatter dateFromString:lblDate.text];
datePicker.minimumDate=date;
NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDate *currentDate = [NSDate date];
NSDateComponents *comps = [[[NSDateComponents alloc] init] autorelease];
[comps setYear:0];
NSDate *maxDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
[comps setYear:-5];
NSDate *minDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
[datePicker setMaximumDate:maxDate];
[datePicker setMinimumDate:minDate];
it showing five year date.......
See the minimum time from the current date to one month:
NSDate* currentTime = [NSDate date];
[datePicker setMinimumDate:[currentTime dateByAddingTimeInterval:43200]];//min time +12:00 for the current date
[datePicker setMaximumDate:[currentTime dateByAddingTimeInterval:2592000]]; // max day (+ 30 )
精彩评论