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
Swift 3: Type 'Any' Has No Subscript Members
How to Get Data from Firebase in Descending Order of Value
Clear Background for Form Sections in Swiftui
Auto Login Dropbox Account on Core API Without Login Prompt
App Not Sized Properly iOS 8 iPhone Simulator
What's a Redirect URI? How Does It Apply to iOS App for Oauth2.0
How to Use a Custom Font with Dynamic Text Sizes in iOS7
How to Create a Big, Red Uibutton with iOS
What's the Uiscrollview Contentinset Property For
Pull to Refresh Uitableview Without Uitableviewcontroller
How to Use Presentmodalviewcontroller to Create a Transparent View
Getting Time Elapsed in Objective-C
Reasons for Rejecting iPhone Application by Apple Store
Uiview - How to Get Notified When the View Is Loaded