If I have a UIDatePicker, and I wish to set the minimum and maximum date range to be between thirty years ago and thirty years in the future, how would I set that up?
8 Answers
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

- 31,094
- 6
- 92
- 116
-
Isn't it going to mess up if you have day and months column as well? It will limit the months and days to current date, in its current form. Like today is 3rd June 2015. years will be fine. Day will be stuck at max value 3 and month will be stuck at max value june – NSNoob Jun 03 '15 at 07:25
-
No. Are you just speculating blindly? – warrenm Jun 05 '15 at 20:18
-
No, I tried that in an app I am working on and I faced the issue I told you. In the end I had to put checks on date separately. – NSNoob Jun 09 '15 at 08:04
-
I just tried it on iOS 8 with `datePickerMode` set to `UIDatePickerModeDate` and it works as expected. I don't know what might be going wrong in your implementation, but you might want to post it as a separate question. – warrenm Jun 10 '15 at 21:14
-
I have created an extension for Swift 4.1: please find it from here: https://stackoverflow.com/a/50580289/1298362 – g212gs May 29 '18 at 09:12
m_datePicker.maximumDate = [NSDate date];
You can use this line of code to set max date (Today).

- 5,672
- 42
- 41
-
-
1Actually that will set the maximum date to the exact minute that that line of code is executed – Juan Boero Oct 29 '15 at 16:35
-
It does not allow one to choose a date later than today but the user sees the later dates already. – Onur Tuna Jun 01 '16 at 07:50
My swift 4 version of @toddg is :
let currentDate = Date()
var dateComponents = DateComponents()
let calendar = Calendar.init(identifier: .gregorian)
dateComponents.year = -100
let minDate = calendar.date(byAdding: dateComponents, to: currentDate)
dateComponents.year = -13
let maxDate = calendar.date(byAdding: dateComponents, to: currentDate)
Works great for me !

- 117
- 2
- 7
Adapting @warrenm's answer to Swift:
let currentDate = NSDate()
let dateComponents = NSDateComponents()
dateComponents.year = -13
let minDate = calendar.dateByAddingComponents(dateComponents, toDate: currentDate, options: NSCalendarOptions(rawValue: 0))
dateComponents.year = 30
let maxDate = calendar.dateByAddingComponents(dateComponents, toDate: currentDate, options: NSCalendarOptions(rawValue: 0))
dobPicker.maximumDate = maxDate
dobPicker.minimumDate = minDate

- 2,863
- 2
- 18
- 33
Was looking at this issue today and came up with a solution in Swift 3 that extends UIDatePicker.
extension UIDatePicker
{
/// set the date picker values and set min/max
/// - parameter date: Date to set the picker to
/// - parameter unit: (years, days, months, hours, minutes...)
/// - parameter deltaMinimum: minimum date delta in units
/// - parameter deltaMaximum: maximum date delta in units
/// - parameter animated: Whether or not to use animation for setting picker
func setDate(_ date:Date, unit:NSCalendar.Unit, deltaMinimum:Int, deltaMaximum:Int, animated:Bool)
{
setDate(date, animated: animated)
setMinMax(unit: unit, deltaMinimum: deltaMinimum, deltaMaximum: deltaMaximum)
}
/// set the min/max for the date picker (uses the pickers current date)
/// - parameter unit: (years, days, months, hours, minutes...)
/// - parameter deltaMinimum: minimum date delta in units
/// - parameter deltaMaximum: maximum date delta in units
func setMinMax(unit:NSCalendar.Unit, deltaMinimum:Int, deltaMaximum:Int)
{
if let gregorian = NSCalendar(calendarIdentifier:.gregorian)
{
if let minDate = gregorian.date(byAdding: unit, value: deltaMinimum, to: self.date)
{
minimumDate = minDate
}
if let maxDate = gregorian.date(byAdding: unit, value: deltaMaximum, to: self.date)
{
maximumDate = maxDate
}
}
}
}
The setDate
method will set three values (date, minimum, maximum) of the UIDatePicker
instance.
setMinMax
only sets the minimum and maximum. Minimum and maximum are calculated using the picker's current date.
Unit can be the following values:
- .year
- .month
- .day
- .hour
- .minute
- .second
To set the date with plus and minus thirty years, the code would be:
var datePicker = UIDatePicker()
var date = Date()
datePicker.setDate(date, unit:.year, deltaMinimum:-30, deltaMaximum:30, animated:true)

- 95
- 1
- 10
Please Try this code Ios8+
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
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];

- 461
- 4
- 9
Set Minimum Date As Today's Date
@IBOutlet var datePicker: UIDatePicker!
datePicker.minimumDate = NSDate() as Date
This will make DatePicker auto-scroll to the current date even if scrolled to an older date

- 51
- 2