Convert millisecond/ Unix date time format to actual date time in swift 4

Convert millisecond/ Unix date time format to actual date time in swift 4

1 . From millisecond to date time format and show on a label

            let date = Date(timeIntervalSince1970: (yourDateValue / 1000.0))


            let dateFormatter = DateFormatter()
            dateFormatter.timeZone = TimeZone(abbreviation: "GMT") //Set timezone that you want
            dateFormatter.locale = NSLocale.current
            dateFormatter.dateFormat = "yyyy-MM-dd HH:mm" //Specify your format that you want
            let strDate = dateFormatter.string(from: date)
            
          publishedDate.text = strDate

2 . From unix to date time format and show on a label

            let date = Date(timeIntervalSince1970: (yourDateValue))


            let dateFormatter = DateFormatter()
            dateFormatter.timeZone = TimeZone(abbreviation: "GMT") //Set timezone that you want
            dateFormatter.locale = NSLocale.current
            dateFormatter.dateFormat = "yyyy-MM-dd HH:mm" //Specify your format that you want
            let strDate = dateFormatter.string(from: date)
            
          publishedDate.text = strDate

2 thoughts on “Convert millisecond/ Unix date time format to actual date time in swift 4

  1. just replace this line
    dateFormatter.dateFormat = “yyyy-MM-dd HH:mm”
    with
    dateFormatter.dateFormat = “yyyy-MM-dd HH:mm:ss”

Leave a Reply

Your email address will not be published. Required fields are marked *