0

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!

9
  • I don't have NSdate object. Commented May 27, 2015 at 17:53
  • Or I don't know I think the return value of v["feedDate"].description is String Commented May 27, 2015 at 17:54
  • You do have NSDate object. It is called dateShow. Commented May 27, 2015 at 19:12
  • Check the value of 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. Commented May 27, 2015 at 19:14
  • if i do this: println(dateShow) it's printing nil so i think i dont have String and NSDate neither :( Commented May 27, 2015 at 19:22

2 Answers 2

3

let dateShow = formatter.dateFromString(v["feedDate"].description)

I would first check if v["feedDate"] isn't an optional, because in this case the v["feedDate"].description would probably contain string like

Optional("2015-05-10T18:39:55+02:00") as opposed to 2015-05-10T18:39:55+02:00,

which obviously won't get converted to NSDate...

Edit: Under your original post you provided this information:

this is what it's print: 2015-05-27T12:51:28.27+02:00 but if i do this way: println(v["feedDate"].asDate) it's print this: Optional(2015-05-23 14:57:43 +0000) i'm using this repo for parsing json

You can use this format in NSDateFormatter to parse it:

yyyy-MM-dd'T'HH:mm:ss.AAZZZZZ

It looks like the API you are using actually already knows how to make a date of it with the asDate method. In any case, if you use your own formatter, make sure that you pass the right string to it, not a description of a String?.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you I think this is going to be the solution. Tomorrow I'm gonna try but I'm sure this will work. Thank you!
3

You need to reformat the date you retrive from the JSON to the format you want to use:

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)

I Hope that helps you!

5 Comments

This looks like the correct answer. You need 2 date formatters, 1 to parse the original string format into an NSDate, and the second one to convert the date to the output format. The only thing I see that might need to be changed is adding a dash between the date and time in the output format. (But I'm no expert on date format strings.)
@DuncanC, I was confuse about the dot instead the dash as well but I test it here and worked fine.
The OP says he/she wants final output like "2015.05.10 - 18:39". I make the output format string to be yyyy.MM.dd - HH:mm (with a dash bewtween the date portion and the time portion.)
@DuncanC I miss that dash, but I update it now, thanks for point that out to me!
@solarenqu that could means lots of things, first make sure you have a node called "feedDate", second make sure v["feedDate"].description returns a values, last make sure the value is in the format you are trying to convert. Add some logs using println to see what you are actually receiving and post it here so we can help you to find the best solution

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.