Minimum and maximum date in UIDatePicker

前端 未结 11 1273
不知归路
不知归路 2020-11-30 00:34

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

11条回答
  •  春和景丽
    2020-11-30 01:07

    **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
    

提交回复
热议问题