Can UIDatePicker minimumDate and maximumDate include time?

A UIDatePicker with defined maximum and minimum dates and times is required. Currently, NSDatePicker.minimumDate and .maximumDate seem to only consider date. What is the right way to handle this?

+5
source share
2 answers

NSDates contain both date and time. From the docs:

NSDate objects represent a single point in time.

In the example below, the current date and time are indicated as the minimum value and the time per hour from this value as the maximum value (to check it, just put it in the viewDidLoad of any UIViewController):

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

NSDate Dev Center.

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

All Articles