dark knight dark knight - 18 days ago 11
iOS Question

How to convert milliseconds to date string in swift 3

I am trying to convert milliseconds to date string in swift 3,i tried by setting date fomatter but i am not getting current date string.

var milliseconds=1477593000000

let date = NSDate(timeIntervalSince1970: TimeInterval(milliseconds))
let formatter = DateFormatter()
formatter.dateFormat = "dd-MM-yyyy HH:mm:ss"
formatter.locale = NSLocale(localeIdentifier: "en_US") as Locale!
print(formatter.string(from: date as Date))

output:

22-01-48793 01:30:00

KAR KAR
Answer

Try this,

var date = Date(timeIntervalSince1970: (1477593000000 / 1000.0))
print("date - \(date)")

You will get output as date :

date - 2016-10-27 18:30:00 +0000