“正确的” JSON日期格式

本文翻译自:The “right” JSON date format

I've seen so many different standards for the JSON date format: 我已经看到JSON日期格式的许多不同标准:

"\"\\/Date(1335205592410)\\/\""         .NET JavaScriptSerializer
"\"\\/Date(1335205592410-0500)\\/\""    .NET DataContractJsonSerializer
"2012-04-23T18:25:43.511Z"              JavaScript built-in JSON object
"2012-04-21T18:25:43-05:00"             ISO 8601

Which one is the right one? 哪一个是正确的? Or best? 还是最好的? Is there any sort of standard on this? 有什么标准吗?


#1楼

参考:https://stackoom.com/question/h9uW/正确的-JSON日期格式


#2楼

JSON does not know anything about dates. JSON对日期一无所知。 What .NET does is a non-standard hack/extension. .NET所做的是非标准的hack /扩展。

I would use a format that can be easily converted to a Date object in JavaScript, ie one that can be passed to new Date(...) . 我将使用一种可以轻松转换为JavaScript中的Date对象的格式,即可以传递给new Date(...)的格式。 The easiest and probably most portable format is the timestamp containing milliseconds since 1970. 最简单且可能最可移植的格式是自1970年以来包含毫秒的时间戳。


#3楼

There is no right format ; 没有正确的格式 ; The JSON specification does not specify a format for exchanging dates which is why there are so many different ways to do it. JSON规范没有指定交换日期的格式,这就是为什么有这么多种不同的方式来实现的原因。

The best format is arguably a date represented in ISO 8601 format ( see Wikipedia ); 最好的格式可以说是以ISO 8601格式表示的日期请参阅Wikipedia )。 it is a well known and widely used format and can be handled across many different languages, making it very well suited for interoperability. 它是一种众所周知的且被广泛使用的格式,并且可以在许多不同的语言中进行处理,因此非常适合互操作性。 If you have control over the generated json, for example, you provide data to other systems in json format, choosing 8601 as the date interchange format is a good choice. 例如,如果您可以控制生成的json,则可以json格式将数据提供给其他系统,那么选择8601作为日期交换格式是一个不错的选择。

If you do not have control over the generated json, for example, you are the consumer of json from several different existing systems, the best way of handling this is to have a date parsing utility function to handle the different formats expected. 例如,如果您无法控制生成的json,则您是几个现有系统中json的使用者,那么处理此问题的最佳方法是使用日期解析实用程序功能来处理所需的不同格式。


#4楼

JSON itself does not specify how dates should be represented, but JavaScript does. JSON本身并未指定日期应如何表示,但JavaScript却指定了。

You should use the format emitted by Date 's toJSON method: 应该使用DatetoJSON方法发出的格式:

2012-04-23T18:25:43.511Z

Here's why: 原因如下:

  1. It's human readable but also succinct 它是人类可读的但也很简洁

  2. It sorts correctly 排序正确

  3. It includes fractional seconds, which can help re-establish chronology 它包括小数秒,可以帮助重新建立时间顺序

  4. It conforms to ISO 8601 符合ISO 8601

  5. ISO 8601 has been well-established internationally for more than a decade ISO 8601已经在国际上建立了十多年的历史

  6. ISO 8601 is endorsed by W3C , RFC3339 , and XKCD W3CRFC3339XKCD认可了ISO 8601

That being said , every date library ever written can understand "milliseconds since 1970". 话虽这么说 ,每个日期库都可以理解“自1970年以来的毫秒数”。 So for easy portability, ThiefMaster is right. 因此,为了方便携带, ThiefMaster是正确的。


#5楼

From RFC 7493 (The I-JSON Message Format ) : RFC 7493(I-JSON消息格式)开始

I-JSON stands for either Internet JSON or Interoperable JSON, depending on who you ask. I-JSON代表Internet JSON或可互操作JSON,具体取决于您问谁。

Protocols often contain data items that are designed to contain timestamps or time durations. 协议通常包含旨在包含时间戳或持续时间的数据项。 It is RECOMMENDED that all such data items be expressed as string values in ISO 8601 format, as specified in RFC 3339 , with the additional restrictions that uppercase rather than lowercase letters be used, that the timezone be included not defaulted, and that optional trailing seconds be included even when their value is "00". 建议所有此类数据项均按照RFC 3339中的规定以ISO 8601格式的字符串值表示,并具有其他限制,即使用大写而不是小写字母,时区不默认,并且可选的尾随秒数即使它们的值为“ 00”也要包括在内。 It is also RECOMMENDED that all data items containing time durations conform to the "duration" production in Appendix A of RFC 3339, with the same additional restrictions. 还建议所有包含持续时间的数据项均符合RFC 3339附录A中的“持续时间”,并具有相同的附加限制。


#6楼

There is only one correct answer to this and most systems get it wrong. 只有一个正确的答案,大多数系统都会弄错。 Number of milliseconds since epoch, aka a 64 bit integer. 自纪元以来的毫秒数,又称64位整数。 Time Zone is a UI concern and has no business in the app layer or db layer. 时区是与UI有关的问题,在应用程序层或数据库层中没有事务。 Why does your db care what time zone something is, when you know it's going to store it as a 64 bit integer then do the transformation calculations. 为什么您的数据库为什么要关心什么时区,当您知道它将以64位整数形式存储时,然后进行转换计算。

Strip out the extraneous bits and just treat dates as numbers up to the UI. 去除多余的位,仅将日期视为直到UI的数字。 You can use simple arithmetic operators to do queries and logic. 您可以使用简单的算术运算符进行查询和逻辑。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章