20

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
dark knight
  • 303
  • 1
  • 4
  • 15

3 Answers3

39

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

KAR
  • 3,303
  • 3
  • 27
  • 50
16

How about trying this -

    let milisecond = 1479714427
    let dateVar = Date.init(timeIntervalSinceNow: TimeInterval(milisecond)/1000)
    var dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "dd-MM-yyyy hh:mm"
    print(dateFormatter.string(from: dateVar))
MilanPanchal
  • 2,943
  • 1
  • 19
  • 37
Saheb Roy
  • 5,899
  • 3
  • 23
  • 35
1

Have a look at the documentation of NSDate:

convenience init(timeIntervalSince1970 secs: TimeInterval)

Returns an NSDate object initialized relative to the current date and time by a given number of seconds.

Just convert your milliseconds to seconds and you should get the correct date.

FelixSFD
  • 6,052
  • 10
  • 43
  • 117