The problem of converting IOS swift String to Date with a difference of 8 hours

Scenes:

Using swift in iOS development

Provide a string and convert it into time format. The converted time is 8 hours less.

        let format = DateFormatter.init()
        format.dateFormat = "yyyy-MM-dd HH:mm:ss"
        let start =  format.date(from: "2023-05-15 16:00:05")!
        print("start:\(start)")
start:2023-05-15 08:00:05 +0000

solve: 

        let format = DateFormatter.init()
        format.dateFormat = "yyyy-MM-dd HH:mm:ss"
        //设置时区为UTC
        format.timeZone = TimeZone(identifier: "UTC")
        let start =  format.date(from: "2023-05-15 16:00:05")!
        print("start:\(start)")

Output:

start:2023-05-15 16:00:05 +0000

Guess you like

Origin blog.csdn.net/renxi0/article/details/131740623