0

I have this code:

var d = "22/12/1968 01:10:40"
var form : NSDateFormatter = NSDateFormatter()
form.dateFormat = "dd/MM/yyyy HH:mm:ss"
form.timeZone = NSTimeZone.localTimeZone() //Italy
print(form.dateFromString(d)!)

When I execute it I get this:

1968-12-22 00:10:40 +0000

first thing Why do I get "+0000" at end? and why the data format isn't respected?

Second , the time is wrong. It's like the time is setted on London's time , but instead I setted it on my local time zone ( italy). I tested it on iPhone Simulator

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
gino gino
  • 249
  • 1
  • 4
  • 8

1 Answers1

0
  1. Why the date is wrong?

    var d = "22/12/1968 01:10:40" var form : NSDateFormatter = NSDateFormatter() form.dateFormat = "dd/MM/yyyy HH:mm:ss"

    //until here, there is no "problem"

    form.timeZone = NSTimeZone.localTimeZone() //Italy //it means, the datetime is 22/12/1968 01:10:40 in Italy (GMT +1)

    //Remember that NSDate is absolute time, or it holds GMT+0 time. //As now, in Italy, it's 22/12/1968 01:10:40, then in GMT+0 time, it's 1h later. So, testDate holds 22/12/1968 00:10:40

    NSDate testDate = form.dateFromString(d)!

  2. Why it does not print the same format? Because you did not use your NSDateFormatter object to get back date string but you used print() to print out directly the description of your object NSDate.

Duyen-Hoa
  • 15,384
  • 5
  • 35
  • 44