0

I did an extension for Date that returns a formatted string:

extension Date {
    var myFormattedDate : String {
        let formatter = DateFormatter()
        formatter.timeZone = TimeZone.current
        formatter.dateFormat = "EEEE, MMMM d, y (HH:mm a)"
        return formatter.string(for: self)!
    }
}

On runtime, I set a breakpoint inside the myFormattedDate property.

po self printed:

2017-09-05 08:50:00 +0000

po formatter.string(for: self)! printed:

Tuesday, September 5, 2017 (11:50 AM)"

What could be the problem? Thanks!

Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223
Tom Odell
  • 105
  • 1
  • 1
  • 11

1 Answers1

1

Printing a Date always returns an UTC time, regardless of the local time zone. Just avoid printing a Date object directly if you want to see the date with the proper time zone in your console.

Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223
  • Im not printing the date object, first I'm using `DateFormatter` and getting a wrong time from it. as you can see in my example above. – Tom Odell Sep 05 '17 at 06:53