简体   繁体   English

将最短日期设置为UIDatePicker

[英]Set Minimum date to UIDatePicker

I have a date in UILabel(like 'Dec 14, 2010). 我在UILabel约会(如'2010年12月14日)。 This date i want to set as minimum date of my UIDatepicker. 这个日期我想设置为我的UIDatepicker的最小日期。 How can i do this please reply? 我怎么能这样回复?

Thanks Nishant 谢谢Nishant

Convert your UILabel text in to date format using NSDateFormatter and put that value replacing "Date" in following example: 使用NSDateFormatter将您的UILabel文本转换为日期格式,并将该值替换为以下示例中的“Date”:

UIDatepicker *datePicker;

NSDate *Date=[NSDate date];

datePicker.minimumDate=Date;

You can use NSDateFormatter and use setDateFormat method for setting Date format. 您可以使用NSDateFormatter并使用setDateFormat方法设置日期格式。

[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 )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM