Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can UIDatePicker's minimumDate and maximumDate include time?

Need a UIDatePicker with specific maximum and minimum dates and times. Currently NSDatePicker.minimumDate and .maximumDate appear to only consider the date. What's the proper way to handle this?

like image 581
CBGrey Avatar asked Mar 14 '10 05:03

CBGrey


People also ask

How do you present a date picker?

On an Access form, use the Date Picker to enter the current date. If the field is set up as a Date/Time field, the Date Picker icon appears when you click in the field. Click the icon, and then click the Today button below the calendar.

How do I use DatePicker in Swiftui?

DatePicker in a FormTapping on the date picker row, reveals a DatePicker interface below it, and allows you to pick a date. Confirm a selection by tapping on the date picker row again to hide the selection interface.


2 Answers

NSDates contain both the date and the time. From the docs:

NSDate objects represent a single point in time.

The example below has the current date and time as the minimum value and the time an hour from now as the maximum value (to test it just drop it into 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];

You can read more about NSDate on the Dev Center.

like image 195
Paulo Fierro Avatar answered Oct 06 '22 08:10

Paulo Fierro


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]];
like image 43
tsr Avatar answered Oct 06 '22 09:10

tsr