简体   繁体   中英

Swift date formatting with NSDateFormatter

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!

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!

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?.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM