Internet Explorer中,Json.Net JavaScript的日期和毫秒问题(Int

2019-06-26 18:03发布

我不知道这是否是我失去了一些东西 - 或者IE或Json.Net。

但基本上是这样工作的:

new Date("2012-08-03T12:36:54.743Z")

这个过程出现“无效的日期”错误:

new Date("2012-08-03T12:36:54.74Z")

第二个日期是存储在SQL Server为:

2012-08-03 12:36:54.740

它然后使用Json.Net序列化为JSON - Json.Net没有序列化的日期为2012-08-03T12:36:54.74Z ,有效地斩去最后0。

我的问题(S):

  • 这是IE浏览器的预期行为 - 它需要在毫秒位全部3位工作?
  • 这是Json.Net的预期行为 - 它永远在日期砍掉最后的0?

Answer 1:

I can't tell you whether it is intended or not, but I've done a lot of searching and I haven't found a real solution for this issue neither. It seems that we have to accept the fact that IE only accepts exactly three digits. The only way (for me) to get around this issue is to use a custom converter for Json.NET when serializing:

string json = JsonConvert.SerializeObject(
    whatEver,
    new IsoDateTimeConverter
        {
            DateTimeFormat = "yyyy-MM-dd\\THH:mm:ss.fffK"
        }
    );

(only tested with Json.NET 4.0.8 and 4.5.8)

This forces Json.NET to use exactly 3 decimal places.

As far as I can tell, Json.NET serializes DateTime values in ISO format with the maximum necessary precision omitting trailing zeroes in the decimal places of the "second" value.

This matches the output of

someDateTimeValue.ToString("yyyy-MM-dd\\THH:mm:ss.FFFFFFFK")
  • A normal DateTime like DateTime.UtcNow will be serialized with up to 7 digits, because that is the precision of a DateTime (measured in Ticks).
  • If the "second" part of the DateTime has less decimal places, Json.NET omits those trailing zeroes.
  • A date value like DateTime.Today will therefore contain no digits behind the "second" value because it is exactly 0.

See also this description of custom date and time format strings.



Answer 2:

这是IE浏览器。 最全面的解释和答案,我发现是JavaScript的:哪些浏览ISO-8601的日期字符串Date.parse的支持解析

特别是

IE9是失败上具有比其他3位计数毫秒:(固定在IE10)



文章来源: Internet Explorer, Json.Net JavaScript date and milliseconds issue