Minimum and Maximum Date in Uidatepicker

Minimum and maximum date in UIDatePicker

Try this:

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *currentDate = [NSDate date];
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setYear:-18];
NSDate *minDate = [gregorian dateByAddingComponents:comps toDate:currentDate options:0];
[comps setYear:-150];
NSDate *maxDate = [gregorian dateByAddingComponents:comps toDate:currentDate options:0];
[comps release];

self.datePicker.minimumDate = minDate;
self.datePicker.maximumDate = maxDate;

It may be easily translated to Swift 4.2:

    let calendar = Calendar(identifier: .gregorian)

let currentDate = Date()
var components = DateComponents()
components.calendar = calendar

components.year = -18
components.month = 12
let maxDate = calendar.date(byAdding: components, to: currentDate)!

components.year = -150
let minDate = calendar.date(byAdding: components, to: currentDate)!

picker.minimumDate = minDate
picker.maximumDate = maxDate

UIDatePicker, setting maximum and minimum dates based on todays date

Not tested, but you probably want something like this.

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *currentDate = [NSDate date];
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setYear:30];
NSDate *maxDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
[comps setYear:-30];
NSDate *minDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];

[datePicker setMaximumDate:maxDate];
[datePicker setMinimumDate:minDate];

Update for Swift 4.1

let calendar = Calendar(identifier: .gregorian)
var comps = DateComponents()
comps.year = 30
let maxDate = calendar.date(byAdding: comps, to: Date())
comps.year = -30
let minDate = calendar.date(byAdding: comps, to: Date())
datePicker.maximumDate = maxDate
datePicker.minimumDate = minDate

UIDatePicker, dates beyond maximum and minimum are still visible

Sorry, but UIDatePicker doesn't support setting valid date ranges like you describe. The only option is prepare an array from calendar dates then pass it to picker.

Xamarin iOS UIDatePicker minimum and maximum dates

The order of the parameters min and max should be the other way around I think. So first the max and second the min:

var startingTime = DateTime.Now;
DateTime min = DateTime.Now.AddDays(-10);
DateTime max = DateTime.Now.AddDays(10);

var dialog = new DT.iOS.DatePickerDialog.DatePickerDialog();

dialog.Show("Choose time", "Done", "Cancel", UIDatePickerMode.Date, (dt) =>
{
string selectedDate = dt.ToString();
}, startingTime, max, min);

UIDatePicker setting minimum and maximum hour

Hope the following code help you.

    - (IBAction)dataPickerValueChanged:(UIDatePicker *)sender {

NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:NSHourCalendarUnit fromDate:[testDatePicker date]];


if ([dateComponents hour] > 8 && [dateComponents hour] < 22) {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Error!" message:@"This time cant be selected" delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles: nil];
[alert show];
return;
}

lblDate.text = [sender date];

}


Related Topics



Leave a reply



Submit