1

I am trying to convert the timestamp from server and it is converting also but only month is coming wrong.Like timestamp is 1492747393892 and it convert into 21/03/17 - 12:03PM but it should be 21/04/17 - 12:03PM. Here is my code

var arriveTimestamp: Int 
if let timeStampToDate = arriveTimestamp {
                    let timeSt = Date(jsonTimeDate:"/Date(\(timeStampToDate))/")
                    let time = Date().dateTime(date: timeSt!)
                    valueLbl.text = time
                }

   init?(jsonTimeDate: String) {
    //        "/Date(1487058855745)/"

    let prefix = "/Date("
    let suffix = ")/"
    let scanner = Scanner(string: jsonTimeDate)

    // Check prefix:
    guard scanner.scanString(prefix, into: nil)  else { return nil }

    // Read milliseconds part:
    var milliseconds : Int64 = 0
    guard scanner.scanInt64(&milliseconds) else { return nil }
    // Milliseconds to seconds:
    var timeStamp = TimeInterval(milliseconds)/1000.0

    // Read optional timezone part:
    var timeZoneOffset : Int = 0
    if scanner.scanInt(&timeZoneOffset) {
        let hours = timeZoneOffset / 100
        let minutes = timeZoneOffset % 100
        // Adjust timestamp according to timezone:
        timeStamp += TimeInterval(3600 * hours + 60 * minutes)
    }

    // Check suffix:
    guard scanner.scanString(suffix, into: nil) else { return nil }

    // Success! Create NSDate and return.
    self.init(timeIntervalSince1970: timeStamp)
}

func dateTime(date: Date) -> String {
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "dd/mm/yy - hh:mm a"
    return dateFormatter.string(from: date as Date)
}
manku
  • 13
  • 1
  • 2
  • 8
  • Your code does not compile as it stands, probably the `init?(jsonTimeDate: String)` method (which looks surprisingly similar to the code in http://stackoverflow.com/a/28854733/1187415 :) and the `dateTime` method should be inside `extension Date { }`. – Martin R Apr 21 '17 at 06:00
  • 2
    Apart from that, your date format for the month is just wrong, it should be "MM", not "mm". – Martin R Apr 21 '17 at 06:00
  • It worked by changing "mm" to "MM". Thanks for the help. – manku Apr 21 '17 at 06:06
  • Note that your conversion code is far too complicated and that JSON Date stuff not needed at all. The time stamp is in milliseconds and you only have to divide it by 1000, compare http://stackoverflow.com/a/30109484/1187415. – Martin R Apr 21 '17 at 06:10
  • Thanks. I'll do that changes. – manku Apr 21 '17 at 06:14

1 Answers1

2

The main error in your code is the wrong date format for the month, which should be "MM", not "mm" (which is for the minutes).

Apart from that, your approach is far too complicated. All you have to do is to divide the timestamp (which is in milliseconds) by 1000 and call Date(timeIntervalSince1970:):

let arriveTimestamp = 1492747393892
let date = Date(timeIntervalSince1970: TimeInterval(arriveTimestamp)/1000)
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd/MM/yy - hh:mm a"
let text = dateFormatter.string(from: date)

print(text) // 21/04/17 - 06:03 AM
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382