Swift ISO8601 format to Date

前端 未结 5 1598
庸人自扰
庸人自扰 2021-01-18 19:20

I have the following string: 20180207T124600Z

How can I turn this into a Date object?

Here is my current code but it returns nil:<

5条回答
  •  隐瞒了意图╮
    2021-01-18 19:52

    You can use following function to get date...

    func isodateFromString(_ isoDate: String) -> Date? {
    
        let formatter = DateFormatter()
        formatter.dateFormat = "yyyyMMdd'T'HHmmssZ"
        return formatter.date(from: isoDate)
    }
    

提交回复
热议问题