Swift 3.0 : Convert server UTC time to local time and vice-versa

后端 未结 7 1185
一生所求
一生所求 2020-12-02 08:09

I want to convert server UTC time to local time and vice-versa. Here is my code..

var isTimeFromServer = true
var time:String!
var period:String!
let timeStr         


        
7条回答
  •  有刺的猬
    2020-12-02 08:56

    You can use in swift 4/5

      var myDate:String = "2020-02-18 14:30:57"
        var convertedLocalTime:String = ""
    
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "yyyy-MM-dd H:mm:ss"
    
        dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
    
        if let dt = dateFormatter.date(from: myDate) {
            dateFormatter.timeZone = TimeZone.current
            dateFormatter.dateFormat = "yyyy-MM-dd h:mm a"
            convertedLocalTime = dateFormatter.string(from: dt)
        } else {
            print("There was an error decoding the string")
        }
    
        print("convertedLocalTime--",convertedLocalTime)
    

提交回复
热议问题