0

I want to convert string to date everything is fine but still, it gives me an incorrect date according to string.

Code:

import UIKit

public class Utils {

    public class func converServerTimeStampToDate (_ timeStamp: String) -> Date {

        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "MM/dd/yyyy, hh:mm:ss a"

        return dateFormatter.date(from: timeStamp)!
    }
}


print(Utils.converServerTimeStampToDate("12/06/2017, 06:48:03 am"
))   

-----OutPut-----

2017-12-06 14:48:03 +0000
halileohalilei
  • 2,220
  • 2
  • 25
  • 52
Nasir Javed
  • 315
  • 2
  • 3
  • 13
  • 1
    why you have tag android and java ? – Ketan Parmar Dec 28 '17 at 05:48
  • you can read this [post](https://stackoverflow.com/a/47942040/6822622), this is helpful for you – AshvinGudaliya Dec 28 '17 at 05:51
  • date has no proper format , what the OP you expect – Anbu.Karthik Dec 28 '17 at 05:52
  • You date it is correct. – Leo Dabus Dec 28 '17 at 05:52
  • Note that when parsing fixed format dates you should set the date formatter locale to `"en_US_POSIX"` – Leo Dabus Dec 28 '17 at 05:53
  • java and android can also be helpfull regarding time formatter – Nasir Javed Dec 28 '17 at 05:57
  • **Fixed-format dates, you should first set the locale of the date formatter to something appropriate for your fixed format. In most cases the best locale to choose is en_US_POSIX, a locale that's specifically designed to yield US English results regardless of both user and system preferences. en_US_POSIX is also invariant in time (if the US, at some point in the future, changes the way it formats dates, en_US will change to reflect the new behavior, but en_US_POSIX will not), and between platforms (en_US_POSIX works the same on iPhone OS as it does on OS X, and as it does on other platforms)** – Leo Dabus Dec 28 '17 at 06:00
  • What is the timezone of your date string? If your date string it is UTC time you need to set the formatter time zone `dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)` – Leo Dabus Dec 28 '17 at 06:04

2 Answers2

0

It is correct result. When you convert any string into Date it will convert it in UTC timezone. And when you convert that date into string again it will be in your current timezone. So, convert that date into string and you real date you will get. So, your Date object always be in UTC timezone and your string will be always in your local timezone. And when you displays your date in label or any other control then definitely you will display in string format and it will be in your local timezone. So, there is nothing wrong in it. You just required to understand the concept!

Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75
  • You mean when you print the date – Leo Dabus Dec 28 '17 at 05:53
  • yeah, when we use date to display it to users then we have to first convert it in to string! @LeoDabus – Ketan Parmar Dec 28 '17 at 05:55
  • Note that the result it is correct but his code it isn't. Look at parsing date strings and look at the locale that he should be using https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html – Leo Dabus Dec 28 '17 at 05:56
  • Another issue he might have it is that the date string that comes from the server it is probably UTC time so he would need to set the date formatter's timezone to zero seconds from GMT – Leo Dabus Dec 28 '17 at 06:02
  • so can you improve the code i want to get the same time that is in the string – Nasir Javed Dec 28 '17 at 06:07
  • Is your date string UTC time? just add `dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)` – Leo Dabus Dec 28 '17 at 06:08
  • don't forget to set the locale `dateFormatter.locale = Locale(identifier: "en_US_POSIX")` – Leo Dabus Dec 28 '17 at 06:09
  • @NasirJaved : convert that date which you get in string with same `dateformatter` and you will get same time string! – Ketan Parmar Dec 28 '17 at 06:09
  • @Lion You are assuming that the string which it is coming from the server it is local time which it is very unlikely – Leo Dabus Dec 28 '17 at 06:11
  • @LeoDabus : Yeah you are right ! It is unlikely but as questioner asking it seems like that! – Ketan Parmar Dec 28 '17 at 06:13
  • @Lion thats why I didnt post any answer. The question it is unclear :) – Leo Dabus Dec 28 '17 at 06:14
  • @Lion ok let me check it – Nasir Javed Dec 28 '17 at 06:15
  • @LeoDabus there are nothing unclear the code is in front of you and the string which i convert is basically the JSON responce from server . all i need is to convert this string into date – Nasir Javed Dec 28 '17 at 06:19
0

To solve this problem set timezone

dateFormatter.timeZone = TimeZone(abbreviation: "GMT+0:00")

By default it is taking current time zone that's why your output is different from input.

Indrajeet
  • 5,490
  • 2
  • 28
  • 43