Stefan DeClerck Stefan DeClerck - 5 months ago 13
iOS Question

How would i convert this specific date string to type NSDate in Swift?

How would I convert this specific

NSDate
,
2016-06-29T23:00:00-07:00
, to an
NSDate
.

Currently I am looking at this website (http://waracle.net/iphone-nsdateformatter-date-formatting-table/) at the
NSDate
Formatting Table but I still can't convert the string to an
NSDate
successfully.

Answer

You can do something like this:

let yourDate = "2016-06-29T23:00:00-07:00" //Your date in string.
let dates = NSDateFormatter()
dates.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
if let someVar = dates.dateFromString(yourDate){
print(someVar) //to avoid optional date.
}
Comments