我有问题在swift中使用uidatepicker设置最大/最小日期

时间:2018-01-29 12:17:40

标签: swift uidatepicker

我想让UIDatePicker显示我选择的两天之间的选择以及时间,例如我选择2018-02-10 ~ 2018-02-1410:00 ~ 12:00 timeInterval = 15,它可以显示那些只有9次选择的5天。

我现在正在使用.minimumDate.maximumDate,但我可以让它从.minimumDate开始到.maximumDate,间隔15分钟。这意味着我可以选择2018-02-12 18:45我不想被选中。

有人能告诉我另一种方式吗?

我很遗憾因为这么愚蠢而错过我的代码。 以下是我正在使用的代码:

    let now = Calendar.current.dateComponents([.year, .month, .day, .hour], from: Date())
    let tmr = Calendar.current.dateComponents([.year, .month, .day], from: Date(timeInterval: 86400, since: Date()))
    let tdat = Calendar.current.dateComponents([.year, .month, .day], from: Date(timeInterval: 172800, since: Date()))
    let twodat = Calendar.current.dateComponents([.year, .month, .day], from: Date(timeInterval: 259200, since: Date()))
    let deadlineTime = String(describing: (store.orderDeadline?.split(separator: ",")[0])!)
    let deadTime = Int(deadlineTime)!
    let noJump = now.hour! < deadTime ? true : false
    let wanttimeDatePicker = UIDatePicker(frame: CGRect(x: 0, y: UIScreen.main.bounds.size.height - 200, width: UIScreen.main.bounds.size.width, height: 200))
    wanttimeDatePicker.datePickerMode = .dateAndTime
    wanttimeDatePicker.minuteInterval = (district.timeInterval?.decoded)!
    wanttimeDatePicker.locale = Locale(identifier: "zh_TW")
    wanttimeDatePicker.addTarget(self, action: #selector(wantTimePickerChanged), for: .valueChanged)
    let formatter = DateFormatter()
    formatter.dateStyle = .short
    formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
    var firstOrderTime: Date!
    if store.orderStart == "+1 days" {
        firstOrderTime = noJump ? formatter.date(from: "\(tmr.year!)-\(tmr.month!)-\(tmr.day!) \(store.firstOrderTime ?? district.startTime!):00")! : formatter.date(from: "\(tdat.year!)-\(tdat.month!)-\(tdat.day!) \(store.firstOrderTime ?? district.startTime!):00")!
    } else if store.orderStart == "+2 days" {
        firstOrderTime = noJump ? formatter.date(from: "\(tdat.year!)-\(tdat.month!)-\(tdat.day!) \(store.firstOrderTime ?? district.startTime!):00")! : formatter.date(from: "\(twodat.year!)-\(twodat.month!)-\(twodat.day!) \(store.firstOrderTime ?? district.startTime!):00")!
    } else {
        firstOrderTime = noJump ? formatter.date(from: "\(now.year!)-\(tmr.month!)-\(tmr.day!) \(store.firstOrderTime ?? district.startTime!):00")! : formatter.date(from: "\(tmr.year!)-\(tmr.month!)-\(tmr.day!) \(store.firstOrderTime ?? district.startTime!):00")!
    }
    wanttimeDatePicker.date = (wantTimeTextField.text! == "" ? firstOrderTime : formatter.date(from: wantTimeTextField.text!))!
    wanttimeDatePicker.minimumDate = firstOrderTime
    wantTimeTextField.inputView = wanttimeDatePicker

1 个答案:

答案 0 :(得分:0)

要在datePicker中设置范围,您可以尝试使用此方法。它在swift 4中为我工作。

    let currentDate = Date()
    var dateComponents = DateComponents()
    let calendar = Calendar.init(identifier: .gregorian)
    dateComponents.year = -100
    let minDate = calendar.date(byAdding: dateComponents, to: currentDate)
    dateComponents.year = -13
    let maxDate = calendar.date(byAdding: dateComponents, to: currentDate)


    datePicker.maximumDate = maxDate
    datePicker.minimumDate = minDate