NSDateFormatter for datetime [duplicate]

2020-05-10 07:23发布

I have a string which is coming from an API that I need to convert into a NSDate but I'm uncertain which dateFormat to use for NSDateFormatter.

let openedAt = "2015-06-30T12:34:00.000-04:00" // coming from API

let formatter = NSDateFormatter()
formatter.dateFormat = "yyyy-MM-dd..." // not sure what format to use here

if let date = formatter.dateFromString(openedAt) {
    println(date)
} else {
    println("NOPE!")
}

1条回答
Fickle 薄情
2楼-- · 2020-05-10 08:16

try

 "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"

The Date Format Patterns guide suggests that "S" is the format specifier for fractions of seconds.

查看更多
登录 后发表回答