需要具有特定最大和最小日期和时间的UIDatePicker。目前NSDatePicker.minimumDate和.maximumDate似乎只考虑日期。处理这个问题的正确方法是什么?
答案 0 :(得分:7)
NSDates包含日期和时间。来自文档:
NSDate对象代表单个时间点。
以下示例将当前日期和时间作为最小值,将现在一小时后的时间作为最大值(测试它只是将其放入任何UIViewController的viewDidLoad中):
// Time right now
NSDate *minDate = [NSDate new];
// One hour from now
NSDate *maxDate = [[NSDate alloc] initWithTimeIntervalSinceNow:60*60];
UIDatePicker *picker = [UIDatePicker new];
[picker setDatePickerMode:UIDatePickerModeDateAndTime];
[picker setMinimumDate:minDate];
[picker setMaximumDate:maxDate];
[[self view] addSubview:picker];
[super viewDidLoad];
您可以在Dev Center上了解有关NSDate的更多信息。
答案 1 :(得分:1)
NSDateFormatter *FormatDate = [[[NSDateFormatter alloc] init] autorelease];
[FormatDate setDateFormat:@"MMMM d y"];
NSString *date1=[FormatDate stringFromDate:[theDatePicker date]];
NSString *today=[FormatDate stringFromDate:[NSDate date]];
NSTimeInterval secondsPerDay = 24 * 60 * 60;
NSDate *tomorrow = [NSDate dateWithTimeIntervalSinceNow:secondsPerDay];
NSString *tommorow=[FormatDate stringFromDate:tomorrow];
[pickerViewPopup dismissWithClickedButtonIndex:0 animated:YES];
if ([date1 isEqualToString:today]||[date1 isEqualToString:tommorow] ) {
NSDateFormatter *FormatDate = [[[NSDateFormatter alloc] init] autorelease];
[FormatDate setDateFormat:@"MMM dd,yyyy"];
currentdate.text = [FormatDate stringFromDate:[theDatePicker date]];