iPhone UIDatePicker 设置MaximumDate

发布于 2024-09-17 07:38:56 字数 292 浏览 2 评论 0原文

我正在尝试构建一个日期选择器,其中今天作为最小日期,2011 年 2 月 1 日作为最大日期。

我已按如下方式设置了最小日期

[picker setMinimumDate: [NSDate date]];

,效果很好,但最大日期似乎不正确。

[picker setMaximumDate: [NSDate dateWithNaturalLanguageString:@"11/02/01"]];

如何正确设置最大日期?

I am trying to build a datepicker which has today as a minimum date and 1 Feb 2011 as Maximum date.

I have set the minimum date as followed

[picker setMinimumDate: [NSDate date]];

and this works just fine but the MaximumDate does not seem to be correct.

[picker setMaximumDate: [NSDate dateWithNaturalLanguageString:@"11/02/01"]];

How do I set the maximum date correctly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

淡水深流 2024-09-24 07:38:56

找到了答案。不管怎样,谢谢你的帮助。

[picker setDatePickerMode:UIDatePickerModeDate];

NSDateFormatter* formatter1 = [[NSDateFormatter alloc] init];
[formatter1 setLenient:YES];
[formatter1 setDateStyle:NSDateFormatterShortStyle];
[formatter1 setTimeStyle:NSDateFormatterShortStyle];

NSString* dateString = @"February 1 2011 10:00am";
NSDate* maxDate = [formatter1 dateFromString:dateString];

[picker setMinimumDate: [NSDate date]];
[picker setMaximumDate: maxDate];

Found the answer. Thanks anyway for your help.

[picker setDatePickerMode:UIDatePickerModeDate];

NSDateFormatter* formatter1 = [[NSDateFormatter alloc] init];
[formatter1 setLenient:YES];
[formatter1 setDateStyle:NSDateFormatterShortStyle];
[formatter1 setTimeStyle:NSDateFormatterShortStyle];

NSString* dateString = @"February 1 2011 10:00am";
NSDate* maxDate = [formatter1 dateFromString:dateString];

[picker setMinimumDate: [NSDate date]];
[picker setMaximumDate: maxDate];
够运 2024-09-24 07:38:56
 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];

听到显示日期 当前日期到 minDate 以及五年(向后)到 maxDate
它只展示了五年......

 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];

hear showing date current date to minDate and five year(back) to maxDate
it's show only five year.....

苦行僧 2024-09-24 07:38:56

您也可以在 Interface Builder 的调色板中执行此操作。我认为如果您按“Command-1”,它将切换到调色板中的一个选项卡,允许您设置选择器的最大日期。

You can also do it in Interface Builder's palette. I think if you press "Command-1" it will switch to a tab in the palette that allows you to set the Maximum date for the picker.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文