I get nil when using NSDateFormatter in swift

后端 未结 2 491
傲寒
傲寒 2021-01-15 22:52

This is my code :

println(myStringDate)    // 2015-02-26T17:46:34.000Z
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = \"YYYY-MM-DDTHH:mm:ss         


        
2条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-15 23:06

    I know that it's to late, but

    dateFormatter.dateFormat = "YYYY-MM-DD'T'HH:mm:ss.sss'Z'"
    

    should be

    dateFormatter.dateFormat = "YYYY-MM-DD'T'HH:mm:ss.SSS'Z'"
    

    because,

    1)

    ss - seconds
    SSS - fractional second
    
    sss != SSS
    

    2)

      T -> 'T'; S -> 'S'
    

    And nice tutorial

提交回复
热议问题