0

I am getting date in the form of milliseconds from URL Request

It is is this form in JSON Dictionary like this:

AllocationDate = "/Date(1514485800000)/";

I am using the following code to convert it to yyyy-MM-dd format:

let data4 = data_object1["AllocationDate"] as! String
let start = data4.index(data4.startIndex, offsetBy: 6)  
let end = data4.index(data4.endIndex, offsetBy: -2)
let range = start..<end
let milliseconds = Double(data4.substring(with: range))
let date = Date(timeIntervalSince1970: (milliseconds!/1000))
let dateString = String(describing: date)
print(dateString)

Now, it prints 2017-12-28 18:30:00 +0000, whereas the real date is 2017-12-29.

Check it on:https://www.timecalculator.net/milliseconds-to-date

So every date is getting converted to previous date of the actual date.

Please help me.If i'm making some mistake in my code or something. Thank You!

rmaddy
  • 314,917
  • 42
  • 532
  • 579
NewCoder
  • 31
  • 1
  • 5
  • Your code is fine and it is printing the correct value. – rmaddy Feb 07 '18 at 17:34
  • timezone. Just set it to yours. – marko Feb 07 '18 at 17:40
  • @soemarko Where? There's no timezone needed in the code in the question. The posted code is perfectly fine. The only issue is the OP's misunderstanding of reading the output of converting a `Date` to a `String` using `String(describing:)`. – rmaddy Feb 07 '18 at 17:43
  • yes, op misunderstood Date. he should've use date formatter to confirm the values. But, _you_ could've pointed this out and help him out rather than making condescending comments. – marko Feb 07 '18 at 17:53
  • Not an answer to your question, but possibly useful: https://stackoverflow.com/a/27909377/1187415 – Martin R Feb 07 '18 at 18:19

0 Answers0