I want to get the minimum and maximum date from a date picker, but minimum date should be \"- 18\" of the current date and the maximum date should be \"- 100\" of current da
Swift 4.0
UIDatePicker Extension
extension UIDatePicker {
func set18YearValidation() {
let currentDate: NSDate = NSDate()
let calendar: NSCalendar = NSCalendar(calendarIdentifier: NSCalendar.Identifier.gregorian)!
calendar.timeZone = NSTimeZone(name: "UTC")! as TimeZone
let components: NSDateComponents = NSDateComponents()
components.calendar = calendar as Calendar
components.year = -18
let maxDate: NSDate = calendar.date(byAdding: components as DateComponents, to: currentDate as Date, options: NSCalendar.Options(rawValue: 0))! as NSDate
components.year = -150
let minDate: NSDate = calendar.date(byAdding: components as DateComponents, to: currentDate as Date, options: NSCalendar.Options(rawValue: 0))! as NSDate
self.minimumDate = minDate as Date
self.maximumDate = maxDate as Date
} }
Another case :
Suppose I want to show Date of birth as a Date picker, Dob is within the range of 1/1/1980 to 1/1/2000.
@IBOutlet var datePicker: UIDatePicker!
override func viewDidLoad() {
super.viewDidLoad()
datePicker.datePickerMode = UIDatePickerMode.date
// 01/01/1980 to 01/01/2000
let calendar = Calendar.current
var minDateComponent = calendar.dateComponents([.day,.month,.year], from: Date())
minDateComponent.day = 01
minDateComponent.month = 01
minDateComponent.year = 1980
let minDate = calendar.date(from: minDateComponent)
print(" min date : \(minDate)")
var maxDateComponent = calendar.dateComponents([.day,.month,.year], from: Date())
maxDateComponent.day = 01
maxDateComponent.month = 01
maxDateComponent.year = 2000
let maxDate = calendar.date(from: maxDateComponent)
print("max date : \(maxDate)")
self.datePicker.minimumDate = minDate! as Date
self.datePicker.maximumDate = maxDate! as Date
}
**Minimum and maximum date in UIDatePicker: #Swift
**if you want to select the min and max date between two date value use below simple code: **
for Example : Min Date : 01-04-2017 and Max Date : 31-03-2018
let calendar = Calendar.current
var minDateComponent = calendar.dateComponents([.day,.month,.year], from: Date())
minDateComponent.day = 01
minDateComponent.month = 04
minDateComponent.year = 2017
let minDate = calendar.date(from: minDateComponent)
print(" min date : \(String(describing: minDate))")
var maxDateComponent = calendar.dateComponents([.day,.month,.year], from: Date())
maxDateComponent.day = 0
maxDateComponent.month = 03 + 1
maxDateComponent.year = 2018
let maxDate = calendar.date(from: maxDateComponent)
print("max date : \(String(describing: maxDate))")
picker.minimumDate = minDate! as Date
picker.maximumDate = maxDate! as Date
Swift 4.1
I have created my extension for all type of limit based on Calendar.Component
extension UIDatePicker {
func setLimit(forCalendarComponent component:Calendar.Component, minimumUnit min: Int, maximumUnit max: Int) {
let currentDate: Date = Date()
var calendar: Calendar = Calendar(identifier: Calendar.Identifier.gregorian)
guard let timeZone = TimeZone(identifier: "UTC") else { return }
calendar.timeZone = timeZone
var components: DateComponents = DateComponents()
components.calendar = calendar
components.setValue(-min, for: component)
if let maxDate: Date = calendar.date(byAdding: components, to: currentDate) {
self.maximumDate = maxDate
}
components.setValue(-max, for: component)
if let minDate: Date = calendar.date(byAdding: components, to: currentDate) {
self.minimumDate = minDate
}
}
}
We can use this as following:
self.datePicker.setLimit(forCalendarComponent: .year, minimumUnit: 13, maximumUnit: 100)
extension UIDatePicker {
func set18YearValidation() {
let currentDate: Date = Date()
var calendar: Calendar = Calendar(identifier: Calendar.Identifier.gregorian)
calendar.timeZone = TimeZone(identifier: "UTC")!
var components: DateComponents = DateComponents()
components.calendar = calendar
components.year = -18
let maxDate: Date = calendar.date(byAdding: components, to: currentDate)!
components.year = -150
let minDate: Date = calendar.date(byAdding: components, to: currentDate)!
self.minimumDate = minDate
self.maximumDate = maxDate
} }