Convert timestamp to Date or Time or DateTime Both


Sat Jan 08 2022 09:39:33 GMT+0000 (UTC)

Saved by @hasnat #ios #swift #date #time #datetime

    func getDateTime(_ timeStamp : String,_ format: String) -> String {
        var t1 = Int(timeStamp) ?? 1
         t1 = t1/1000
        let date = NSDate(timeIntervalSince1970: TimeInterval(t1))
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = format
        let dateString = dateFormatter.string(from: date as Date)
        print("Date = \(dateString)")
        return dateString

if you want to convert timeStamp to DateTime you have to pass timeStamp and DateTime format that you want in return. this func will return you dateTime String.