-1

I have a DateFormatter and I am passing a String of date to it with an intent to transform to date. The problem is that this final date is always coming with 3 hours more. This is my code:

let taskDateFormatter = DateFormatter()
taskDateFormatter.locale = Locale.current                    
taskDateFormatter.dateFormat = "dd/MM/yyyy HH:mm:ss"
print(task!.logData!)// this print 016/06/2017 10:25:12
let date = taskDateFormatter.date(from: task!.logData!)
print(date!)// this print 2017-06-16 13:25:12 +0000

What am I doing wrong?

danh
  • 62,181
  • 10
  • 95
  • 136
breno morais
  • 85
  • 1
  • 11
  • 5
    There is no error. You are in the GMT-3 timezone, and `(NS)Date` always uses UTC for printing (as the "+0000" indicates). – Martin R Jun 16 '17 at 13:32
  • Possible duplicate of https://stackoverflow.com/questions/6229024/nsdate-format-outputting-wrong-date or https://stackoverflow.com/questions/29407599/nsdateformatter-return-wrong-date-swift – Martin R Jun 16 '17 at 13:33
  • Possible duplicate of [NSDate Format outputting wrong date](https://stackoverflow.com/questions/6229024/nsdate-format-outputting-wrong-date) – Stonz2 Jun 16 '17 at 13:40

1 Answers1

3

Try to add

taskDateFormatter.timeZone = NSTimeZone(name: "UTC")
S. Matsepura
  • 1,673
  • 1
  • 17
  • 24
  • Do not do this unless the string being parsed is really in UTC format. Do not do this just to change the output of printing `date`. – rmaddy Jun 16 '17 at 15:04