0

I am getting date string from server like that "2017-08-05T00:30:00.000+02:00". Below is my code for date formatter.

 let dateFormatter: DateFormatter = DateFormatter()
 let serverDateString = "2017-08-05T00:30:00.000+02:00"
 dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZZ"
 dateFormatter.locale = Foundation.Locale(identifier: "en_US_POSIX")
 let currentDate: Date = dateFormatter.date(from: serverDateString)!

But it returns 2017-08-04 22:30:00 +0000. Which is wrong. Any suggestions?

  • in which format do you want your dateformatter – Raksha Saini Jul 29 '17 at 13:03
  • I want it to retuen me lie this 2017-08-05 00:30:00 +0000. It does return me in this format but with incorrect date and time. Right now it is returning 2017-08-04 22:30:00 +0000. Instead it should return 2017-08-05 00:30:00 +0000. – uzair shahzad Jul 29 '17 at 13:16
  • use this http://nsdateformatter.com/ can help you i think – Reinier Melian Jul 29 '17 at 13:18
  • Try this link https://stackoverflow.com/questions/41169923/swift-nsdate-showing-utc-when-printing-in-debugger-how-do-i-know-if-its-local – Hussain Shabbir Jul 29 '17 at 13:32
  • 1
    `2017-08-05T00:30:00.000+02:00` and `2017-08-04 22:30:00 +0000` are the same moment in time expressed in different time zones. Like 7AM in London (GMT+0) is 9AM in Athens (GMT+2). Why do you expect `2017-08-05 00:30:00 +0000`? You want to strip the timezone info from the server's response? – Code Different Jul 29 '17 at 13:36
  • Yes, i want to strip the time zone information from server. I want to display it as it is coming from server. – uzair shahzad Jul 29 '17 at 13:43

6 Answers6

0
yyyy-MM-dd'T'hh:mm:ss.SSSZZZZZZ

This is correct date formate for your input string. Here is the Apple Document for more description.

Here is my code:

var strInputDateString: String = "2017-08-05T00:30:00.000+02:00"
var dateFormat = DateFormatter()
dateFormat.dateFormat = "yyyy-MM-dd'T'hh:mm:ss.SSSZZZZZZ"
    //Set new dateFormate
var date1: Date? = dateFormat.date(from: strInputDateString)
dateFormat.dateFormat = "dd-MM-YYYY hh:mm:ss"
    // Your Desire
var strOutputDateString: String = dateFormat.string(from: date1!)
print("\(strInputDateString)")
print("\(strOutputDateString)")

output:

Main input String:2017-08-05T00:30:00.000+02:00
Converted String:  05-08-2017 04:00:00

For now, I tried to convert your date format with my custom. It's give me the perfect output.

Nirmalsinh Rathod
  • 5,079
  • 4
  • 26
  • 56
0

Your Code for parsing date is correct: 2017-08-05T00:30:00.000+02:00 and 2017-08-04 22:30:00 +0000 represent the same time, just in different time zones. Your only problem is that Date doesn't actually store the time zone

vp2698
  • 1,783
  • 26
  • 28
0

In Swift 3 You can change your server time string to UTC time Date as:

let dateFormatter: DateFormatter = DateFormatter()
var serverDateString = "2017-08-05T00:30:00.000+02:00"
let index = serverDateString.index(serverDateString.startIndex, offsetBy: 19)
serverDateString = serverDateString.substring(to: index)
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
dateFormatter.timeZone = TimeZone.init(identifier: "UTC")
let currentDate: Date = dateFormatter.date(from: serverDateString)!
Nikhlesh Bagdiya
  • 4,316
  • 1
  • 19
  • 29
  • That worked like a charm. Apparently i had to ignore the time stamp from server, which you did. I was pulling my hairs off all day long yesterday. Thanks alot. – uzair shahzad Jul 30 '17 at 07:58
-1

Try this

let dateFormatter: DateFormatter = DateFormatter()
let serverDateString = "2017-08-05T00:30:00.000+02:00"
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssXXX"
dateFormatter.locale = Foundation.Locale(identifier: "en_US_POSIX")
let currentDate: Date = dateFormatter.date(from: serverDateString)!

Hope this helps

Kamlesh Shingarakhiya
  • 2,757
  • 2
  • 16
  • 34
-1

Actually, your code is good. I copied your data and ran it in playground. What kind of format you want it be?

JsW
  • 1,682
  • 3
  • 22
  • 34
  • Try to run it on device. On device this is the result i get 2017-08-04 22:30:00 +0000. Which is wrong. The date should be 2017-08-05 and time should be 12:30:00 +0000 – uzair shahzad Jul 29 '17 at 12:57
  • I got the same problem as yours before, check this [my old question](https://stackoverflow.com/questions/44177435/why-create-date-from-string-only-failed-on-real-iphone) – JsW Jul 29 '17 at 13:02
  • I checked your answer but i'm already doing this, dateFormatter.locale = Foundation.Locale(identifier: "en_US_POSIX") – uzair shahzad Jul 29 '17 at 13:12
  • Actually, `08-05 00:30:00 +02:00` is equal to `-08-04 22:30:00 +00:00`. – JsW Jul 29 '17 at 13:33
  • I'm not familiar with Date APIs, but if you simply want to get the date, this could help `Date(timeInterval: -7200, since: currentDate)`. The server returned a date in GMT +02:00, but you need a GMT +00:00 date, so I just advanced date by -7200 seconds. – JsW Jul 29 '17 at 13:48
-1

The step you are missing is to again use your DateFormatter to format the date into a string. When you use print on the date, it will always show GMT. For illustration purposes, I've set the TimeZone to Berlin to match the original offset of +2:00.

let dateFormatter: DateFormatter = DateFormatter()
let serverDateString = "2017-08-05T00:30:00.000+02:00"
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZZ"

// Hard set to Berlin
dateFormatter.timeZone = TimeZone(identifier: "Europe/Berlin")
let currentDate: Date = dateFormatter.date(from: serverDateString)!

// Your missing step
dateFormatter.string(from: currentDate)

Remember that DateFormatters are about:

  • Parsing from a String - df.date(from: String)
  • Formatting from a Date - df.string(from: Date)
applejack42
  • 1,155
  • 9
  • 18