我有以下问题。我的代码应该在按下按钮时触发 DatePickerView。(在代码中它被称为“deadLineTestButton”)并且当用户成功选择日期和时间时,控制台会打印一个完全不同的时间。
这是我使用的代码
@IBAction func deadlineTestButton(_ sender: UIButton) {
var savedDate = ""
var chosenDate:Date = Date()
let alert = UIAlertController(style: .actionSheet, title: "Select date")
alert.addDatePicker(mode: .dateAndTime , date: Date()) { (date) in
// Do something with the date
print("Choosing date : \(date)")
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .medium
dateFormatter.timeStyle = .none
dateFormatter.locale = Locale(identifier: "en_US")
savedDate = dateFormatter.string(from: date)
chosenDate = date
}
let saveDate = UIAlertAction(title: "Save", style: .default) { (action) in
// Calculte differnece in dates
// Saved date is the homework's due date
let currentDate:Date = Date()
// Diffference from days
let diffInDays = Calendar.current.dateComponents([.day], from: currentDate, to: chosenDate).day
self.deadLineLabel.text = String(diffInDays!)
print(savedDate)
if self.courseTextField.text == "Economia" {
self.economy.deadline = chosenDate
} else if self.courseTextField.text == "Matematicas" {
self.matematicas.deadline = chosenDate
} else if self.courseTextField.text == "Escritura" {
self.escritura.deadline = chosenDate
} else if self.courseTextField.text == "Herramientas"{
self.herramientas.deadline = chosenDate
} else if self.courseTextField.text == "Autococimiento" {
self.autoconocimiento.deadline = chosenDate
} else if self.courseTextField.text == "Administración" {
self.administracion.deadline = chosenDate
}
}
当我尝试运行我的代码时会发生这种情况
在第一张照片中,我选择的时间是上午 12:38,但在控制台中,它记录为 17:38:07