【问题标题】:Error: Datepicker shows one year ahead in the UITextfield.inputview when using "MM/dd/YYYY" format [duplicate]错误:使用“MM/dd/YYYY”格式时,日期选择器在 UITextfield.inputview 中显示提前一年 [重复]
【发布时间】:2020-07-15 17:45:35
【问题描述】:

当使用“MM/dd/YYYY”格式时,UIDatePicker 在 UITextfield.inputview 中显示提前一年

@objc public func dateChange(datePicker: UIDatePicker) {
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "MM/dd/YYYY"
    dateOfBirthVC.customView.entryTextField.text = dateFormatter.string(from: datePicker.date)
}

以下是跟踪日期选择器更改并显示在 UITextField 中的代码

datePicker?.addTarget(self, action: #selector(ViewController.dateChange(datePicker:)), for: .valueChanged)
dateOfBirthVC.customView.entryTextField.inputView = datePicker

这就是问题所在。当用户在日期选择器中更改日期时,UITextfield 中的年份显示为一年后。仅当用户将月份设置为 12 月并将日期设置在 25 到 31 之间时才会发生这种情况。

Screenshot

【问题讨论】:

    标签: ios swift uitextfield uidatepicker


    【解决方案1】:

    更改“MM/dd/YYYY”

    到“MM/dd/yyyy”

    【讨论】:

      猜你喜欢
      • 2018-01-13
      • 2016-02-25
      • 1970-01-01
      • 1970-01-01
      • 2012-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-18
      相关资源
      最近更新 更多