UIDatePicker 我想将UIDatePicker设置为始终具有UTC时间和日期,而忽略任何本地时间和夏令时。如果我将在文本字段中将时间设置为02:00,则在2019年3月31日之后的我的应用程序UIDatePicker中,它将显示03:00。
`func createDatePicker() {
datePicker = UIDatePicker()
datePicker?.datePickerMode = .dateAndTime
datePicker?.timeZone = NSTimeZone(name: "GMT")! as TimeZone
datePicker?.timeZone = TimeZone(secondsFromGMT: 0)
datePicker?.setValue(UIColor.white, forKey: "textColor")
datePicker?.backgroundColor = UIColor.black.withAlphaComponent(0.6)
datePicker?.minimumDate = Date.calculateDate(day: 1, month: 1, year: 2019, hour: 0, minute: 0)
datePicker?.maximumDate = Date.calculateDate(day: 1, month: 1, year: 2020, hour: 0, minute: 0)
// create toll bar
let toolBar = UIToolbar()
toolBar.sizeToFit()
toolBar.barStyle = .blackOpaque
//add done botton on this tool bar
let doneBotton = UIBarButtonItem(barButtonSystemItem: .done, target: nil, action: #selector(doneDateClicked))
toolBar.setItems([doneBotton], animated: true)
doneBotton.style = .done
dateTextField.inputAccessoryView = toolBar
dateTextField.inputView = datePicker
}`
答案 0 :(得分:-1)
您可以使用以下功能来获取可能需要用来校正夏令时的偏移量。
datePicker?.timeZone.daylightSavingTimeOffset(for: Date)
返回给定日期的夏令时偏移量。
参数
日期:用于计算的日期。默认值为当前日期。