NSDateFormatter dateFromString Always Returns nil

前端 未结 7 754
不知归路
不知归路 2020-12-06 10:19

I want to apologize ahead of time for asking a repeat question, but none of the other solutions have worked for me yet. Every time I try to pass a date string to the dateFro

7条回答
  •  感动是毒
    2020-12-06 10:32

    According to NSDateFormatter documentation :

    When working with fixed format dates, such as RFC 3339, you set the dateFormat property to specify a format string.

    If your date format is 2017-06-16T17:18:59.082083Z then dateFormat property should look like this yyyy-MM-dd'T'HH:mm:ss.SSSSSSZ.

    Swift 3

    let dateFormatter = DateFormatter()
    let date = "2017-06-16T17:18:59.082083Z"
    dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSSSSZ"
    
    let result = dateFormatter.date(from: date) // 2017-06-16 17:18:59 +0000
    

提交回复
热议问题