简体   繁体   English

将日期格式iso-8601更改为自定义格式

[英]change date format iso-8601 to custom format

I have a json file being parsed using JSONDecoder(). 我有一个使用JSONDecoder()解析的json文件。 However, I receive the variable timestamp of type Date in iso-8601-format ("yyyy-MM-dd'T'HH:mm:ss.SSSXXXXX") but on my view, I want to display it in a custom format: "dd/mm/yy HH:mm:ss". 但是,我收到了iso-8601格式的日期类型的可变时间戳记(“ yyyy-MM-dd'T'HH:mm:ss.SSSXXXXX”),但是在我看来,我想以自定义格式显示它: “ dd / mm / yy HH:mm:ss”。

I have written the following code, but I get nil for timestamp and furthermore I assume "date" is not the correct type to use when timestamp comes in iso-8601 format: 我已经编写了以下代码,但时间戳记为nil,并且我认为时间戳记为iso-8601格式时,“ date”不是正确的类型:

Error json: typeMismatch(Swift.Double, Swift.DecodingError.Context(codingPath: [_JSONKey(stringValue: "Index 0", intValue: 0), CodingKeys(stringValue: "timestamp", intValue: nil)], debugDescription: "Expected to decode Double but found a string/data instead.", underlyingError: nil)) 错误json:typeMismatch(Swift.Double,Swift.DecodingError.Context(codingPath:[_JSONKey(stringValue:“ Index 0”,intValue:0),CodingKeys(stringValue:“ timestamp”,intValue:nil)],debugDescription:“期望解码Double,但找到了一个字符串/数据。”,underlyingError:nil))

swift4 swift4

import UIKit

enum Type : String, Codable {
    case organizational, planning
}

// structure from json file
struct News: Codable{
    let type: Type
    let timestamp: Date //comes in json with ISO-8601-format
    let title: String
    let message: String

    enum  CodingKeys: String, CodingKey { case type, timestamp, title, message}

    let dateFormatter : DateFormatter = {
        let formatter = DateFormatter()
        formatter.dateFormat = "dd/MM/yy HH:mm:ss"  // change format ISO-8601 to dd/MM/yy HH:mm:ss
        return formatter
    }()

    var dateString : String {
        return dateFormatter.string(from:timestamp) // take timestamp variable of type date and make it a string -> lable.text
    }
}

When you are decoding a Date the decoder expects an UNIX timestamp (a Double ) by default, this is what the error message tells you. 当您解码Date ,解码器默认需要UNIX时间戳( Double ),这就是错误消息告诉您的内容。

However you can indeed decode an ISO8601 string as Date if you add the decoder.dateDecodingStrategy = .iso8601 but this decodes only standard ISO8601 strings without milliseconds. 然而,你确实可以解码的ISO8601字符串作为Date如果添加decoder.dateDecodingStrategy = .iso8601但这仅解码标准ISO8601串毫秒。

There are two options: 有两种选择:

  1. Add a formatted dateDecodingStrategy with a DateFormatter . 使用DateFormatter添加formatted dateDecodingStrategy

     let dateFormatter = DateFormatter() dateFormatter.locale = Locale(identifier: "en_US_POSIX") dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSXXXXX" let decoder = JSONDecoder() decoder.dateDecodingStrategy = .formatted(dateFormatter) try decoder.decode(... 
  2. Declare timestamp as timestamp声明为

     let timestamp: String 

    and convert the string back and forth with two formatters or two date formats in dateString . 并使用dateString两个格式化程序或两个日期格式来回转换字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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