Search code examples
iosswiftnsdateformatterunix-timestamp

Getting date and time from unix timestamp


I am trying to get current date and time from a unix time stamp but it keeps giving me the wrong date this is the code i am currenty

 func getDateFromStamp(timeInterval:Int) -> String{
     let date = NSDate(timeIntervalSince1970: TimeInterval(timeInterval))
     let dateFormatter = DateFormatter()
     dateFormatter.dateFormat = "EEEE, MMM d, h:mm a"
     let dateString = dateFormatter.string(from: date as Date)
   return dateString
}

Solution

  • Usually unix timestamps are in milliseconds, but Date uses seconds. You might need to divide your timestamp by 1000.