0

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

The time that I choose is at 12:38pm but in the console, it logs as if it were at 17:38:07

Here it's logging as a different time from when I chose it

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

Francisco Ruiz
  • 209
  • 3
  • 12
  • what you are seeing printed is UTC. if you would like to see the current time some just print its description with locale `date.description(with: .current)` – Leo Dabus Mar 29 '18 at 17:48
  • But how can I save that date in the local time zone? Since I am also trying to calculate the difference between the current day and the date the user picked, both must be in the same time zone so it can be calculated correctly – Francisco Ruiz Mar 29 '18 at 18:14
  • You don’t. If you need to display it localized just use DateFormatter – Leo Dabus Mar 29 '18 at 18:15
  • And how could I use DateFormatter to achieve that? Sorry for the trouble – Francisco Ruiz Mar 29 '18 at 18:29
  • https://stackoverflow.com/questions/28332946/how-do-i-get-the-current-date-in-short-format-in-swift/28347285?s=1|24.1396#28347285 – Leo Dabus Mar 29 '18 at 18:56

1 Answers1

1

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.

Tom Harrington
  • 69,312
  • 10
  • 146
  • 170