我正在尝试此代码,但无法正常工作。
谢谢大家。
我正在将uidatepicker与select minimumDate一起使用。但在datepicker swift 2.0中需要当前日期和额外的15天。

override func viewDidLoad() {
 let datePickerView:UIDatePicker = UIDatePicker()
        datePickerView.datePickerMode = UIDatePickerMode.Date
        sender.inputView = datePickerView
        datePickerView.minimumDate = datePickerView.date

        //datePickerView.maximumDate = datePickerView.date

 datePickerView.addTarget(self, action: #selector(BookAppointmentView.datePickerValueChanged(_:)), forControlEvents: UIControlEvents.ValueChanged)
        }

func datePickerValueChanged(sender:UIDatePicker)->NSDate {

        let baseDate:NSDate = NSDate()

        let dateFormatter = NSDateFormatter()
        dateFormatter.dateFormat = "yyyy-MM-dd"


        DatePickerField.text = dateFormatter.stringFromDate(sender.date)
        print("date of bookAppointment\(DatePickerField.text!)")

       return newDate!

    }

最佳答案

有多种方法可以实现此目的。
dateByAddingTimeInterval为1,您以秒为单位添加时间:

datepickerView.minimumDate = NSDate() // current date
datepickerView.maximumDate = NSDate().dateByAddingTimeInterval(1296000) // date + 15 days


您也可以这样做:

datepickerView.minimumDate = NSDate() // current date
let maxDateComponent = NSDateComponents()
maxDateComponent.day = //days you want to add
datePickerView.maximumDate = NSCalendar.currentCalendar().dateByAddingComponents(maxDateComponent, toDate: NSDate(), options: NSCalendarOptions.init(rawValue: 0))


您以天为单位添加时间。

10-05 21:06
查看更多