This is my code :
println(myStringDate) // 2015-02-26T17:46:34.000Z
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "YYYY-MM-DDTHH:mm:ss.sssZ"
let myDate = dateFormatter.dateFromString(myStringDate)
println(myDate) // nil
Why nil ? What am I doing wrong ?
I know that it's to late, but
should be
because,
1)
2)
And nice tutorial
Your problem is how you set the date format. Because there is a
T
in your string, you need to "escape" it and say, that it shouldn't affect the formatting of the date.Also as you see, I've changed the
sss
toSSS
which stands for miliseconds and also changed the yearYYYY
to lowercaseyyyy
also the same with theDD
. You have to be really carefully, when to use upper and lowercase letters in your Dateformat-strings.Check @HotLicks link in the comments which shows a great overview of the dateformat usage.