Reputation: 209
I have the following problem. My code is supposed to trigger a DatePickerView when a button is pressed. (In code it's referred to "deadLineTestButton") and when the user successfully selects a date and a time, the console prints a completely different time.
Here is the code I used
@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
}
}
This is what happens when I try to run my code
In the first photo, the time that I chose was at 12:38om, but in the console, it logs as if it were 17:38:07
Upvotes: 0
Views: 275
Reputation: 70966
The date picker shows local time at your location. Printing the date displays the UTC time in the console. You would appear to be 7 hours from UTC.
Upvotes: 1