【问题标题】:How do I only allow day and date on Datepicker?我如何只允许 Datepicker 上的日期和日期?
【发布时间】:2016-01-15 06:05:33
【问题描述】:

我有一个日期选择器,我想限制日期选择器显示日期和月份,但是月份不能更改,它附加到日期。即 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

【问题讨论】:

  • 您不能使用UIDatePicker。您需要使用UIPickerView
  • 很不幸,谢谢!

标签: ios swift datepicker


【解决方案1】:

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

但实际上它并不像看起来那么容易。至少因为你不选择年份,所以你不知道二月份有多少天。

【讨论】:

  • @AnkitSrivastava 此解决方案中的关键词是forDate: date,其中 date 包含当前日期和年份。但提问者没有具体说明任何年份。所以二月的问题仍然存在。
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多