How do I format JSON Date String with Swift?

后端 未结 5 1683
Happy的楠姐
Happy的楠姐 2020-12-29 06:55

I make an http get request to a server and get back a json object with a date string like this:

{
    name = \"Place1\";
    temperature = 79;
    humidity =         


        
5条回答
  •  盖世英雄少女心
    2020-12-29 07:40

    If you are using SwiftDate (you probably should!) simply use:

    Serialize:

    let date:NSDate = ....
    let dateInRegion = DateInRegion( absoluteTime: date )
    let serializedString:String = dateInRegin.toString( .ISO8601Format( .Full ))!
    

    Deserialize:

    let serializedDate:String = "2016-03-01T22:10:55.200Z"
    let date:NSDate? = serializedDate.toDateFromISO8601()
    

提交回复
热议问题