我如何只在Datepicker上允许日期和日期?

时间:2016-01-15 06:05:34

标签: ios swift datepicker

我有一个日期选择器,我想限制日期选择器显示日期和日期和月份,但月份不能更改,它附加到日期。即1月15日星期五。1月15日是一个组成部分。它应该显示当天加上接下来的6天,所以一周。我不想要这一年。

 let gregorian: NSCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
    let currentDate: NSDate = NSDate()
    let components: NSDateComponents = NSDateComponents()
    let minDate: NSDate = gregorian.dateByAddingComponents(components, toDate: currentDate, options: NSCalendarOptions(rawValue: 0))!
    components.year = +7
    let maxDate: NSDate = gregorian.dateByAddingComponents(components, toDate: currentDate, options: NSCalendarOptions(rawValue: 0))!
     self.datepicker.minimumDate = minDate
    self.datepicker.maximumDate = maxDate

enter image description here

2 个答案:

答案 0 :(得分:0)

首先,您必须使用两部分UIPickerView,并为月和日部分实施数据源。

但实际上并不像看起来那么容易。至少是因为你不知道2月份有多少天,因为你没有选择一年。

答案 1 :(得分:0)

原谅我的无知,如果我离这里不远,但不能在这里使用NSDateFormatter?

let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MMMM"
convertedDate = dateFormatter.stringFromDate(currentDate)