i have a json object that contains a date from database:
var showDateRaw = v["feedDate"].description
println(showDateRaw)
showDateRaw variable's type is String and looks like this way after parsing the json object: 2015-05-10T18:39:55+02:00
i would like to convert it to look like this way: 2015.05.10 - 18:39
First of all i was trying to convert to NSDate with NSDateFormatter:
var formatter : NSDateFormatter = NSDateFormatter()
formatter.dateFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"
let dateShow = formatter.dateFromString(v["feedDate"].description)
println(dateShow)
And then somehow convert this date to the new format and then convert it to String. But when i try to print the formated dateShow variable it's value is nil..
Could anybody help me how can i convert this String date to a expected format?
Thank you very much!
By the answers i've tried this way:
var formatter = NSDateFormatter()
formatter.dateFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"
let dateShow = formatter.dateFromString(v["feedDate"].description)
var finalFormatter = NSDateFormatter()
finalFormatter.dateFormat = "yyyy.MM.dd HH:mm"
let finalDate = finalFormatter.stringFromDate(dateShow!)
but it throws an error:
fatal error: unexpectedly found nil while unwrapping an Optional value
do you have any idea why is it? Thank you..
UPDATE:
this is the solution, my json parser api had a .asDate method which return with optional NSDate.
let dateShow : NSDate? = v["feedDate"].asDate
var finalFormatter = NSDateFormatter()
finalFormatter.dateFormat = "yyyy.MM.dd - HH:mm"
let finalDate = finalFormatter.stringFromDate(dateShow!)
println(finalDate)
After got it, it was easy to convert to expected format.
Thank you very much for every idea!
v["feedDate"].description. Use println for example. If it is Optional, you can't pass its description to the formatter. Unwrap it toregular string, first.