0

I am trying to parse a date that is sent to the server.

Now the date that is recieved from the server is "2016-05-10T22:34:00.000Z"

And here is my code to get a formatted date out of the above date string :

 let format="yyyy-MM-dd'T'HH:mm:ss.SSSZ"
    let dateFmt = NSDateFormatter()
    dateFmt.dateFormat = format
    let newreadableDate = dateFmt.dateFromString(dateString)
    print(newreadableDate!)
    dateFmt.dateFormat = "MMM dd hh:mm a"
            print("Formatted date is : \(dateFmt.stringFromDate(newreadableDate!))")

The print statement prints the following result : May 11 04:04 AM

The above result is totally wrong. I should get back the same date that I recieved from server but with different format.

But the newReadableDate variable prints the correct date : 2016-05-10 22:34:00 +0000

But after I format it, it gives me wrong date.

What is wrong in the above code ?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
intellignt_idiot
  • 1,962
  • 2
  • 16
  • 23

1 Answers1

0

Your code is fine. The output is correct. The date string you parse is in UTC time. But you log the final result in local time. You must live in a timezone that is 5.5 hours ahead of UTC.

The Z in the date string represents "Zulu" time which it UTC time. So the date formatter parses the string as a time in UTC time.

You then choose to print the resulting NSDate. By default, NSDateFormatter will generate a string from an NSDate in locale time.

May 10, 2016 at 22:34 UTC time is exactly the same time as May 11, 2016 at 04:04 am in your local (+0530) timezone.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • What I don't get was why `newReadableDate` printed the date in UTC. I thought that the `Z` makes the date formatter interpret the string date as UTC, but when printing it, use the local time zone. I'm in UTC-4 and `newreadableDate` is `May 10, 2016, 6:34 PM`. – paulvs May 10 '16 at 18:00
  • 1
    @paulvs `print` calls the `description` method of an object you are printing. The implementation of `NSDate description` is to log the date in UTC time. That's what the `+0000` means. It indicates the time is being shown in the +0000 timezone which is UTC time. – rmaddy May 10 '16 at 18:03
  • that's true. I figured out why it was showing the time in my local timezone - I was using a Playground and the default printing on the right seems to use the local timezone instead of the default UTC timezone. – paulvs May 10 '16 at 19:13
  • 1
    @paulvs Apple really messed this all up. Prior to several versions of iOS and Xcode ago, printing an `NSDate` always showed the date in local time. Then in some prior update they changed the output to be in UTC. I can understand why they did it but the number of questions posted here has really gone up since then due to people not understanding the output being in UTC (and now the Swift playground is back to local time). – rmaddy May 10 '16 at 19:17
  • Thanks @rmaddy for the explanation! – paulvs May 10 '16 at 19:37
  • @rmaddy Now what do I do ? Convert the UTC date to local time zone ? But that is already happening, isnt't it ? Or do I need to set the date formatter to localTime zone before parsing the date string ? – intellignt_idiot May 11 '16 at 02:34
  • @rmaddy It means instead of printing the date in local time zone, I need to set the time zone to UTC again, and then print. I did that and it worked perfectly. Now it means I will be showing the date in exact same format what is shown at the server. But one thing I do not understand. When I send the date to server it is in default format, I get date and format it with nsdateformatter and send it to server. But in UTC also the date remains same. If I send 10:34 PM , it still remains the same on the server. So where does the conversion to UTC takes place ? There should be a 5.5 hr diff? – intellignt_idiot May 11 '16 at 02:45
  • @intellignt_idiot What you are missing is that the `NSDate` has no timezone. The timezone is only an artifact of converting the `NSDate` to a string. Think of it this way. The `NSDate` is simply a point in time. That point represents a different clock time based on a user's timezone but it is all still the same moment. – rmaddy May 11 '16 at 03:07