Internet Explorer, Json.Net JavaScript date and milliseconds issue

后端 未结 2 1420
孤城傲影
孤城傲影 2021-01-02 08:55

I\'m not sure if it\'s me missing something - or IE or Json.Net.

But basically this works:

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

This f

相关标签:
2条回答
  • 2021-01-02 09:14

    It's IE. The most comprehensive explaination and answer I found is at JavaScript: Which browsers support parsing of ISO-8601 Date String with Date.parse

    In particular

    IE9 was failing on milliseconds with digit counts other than 3: (fixed in IE10)

    0 讨论(0)
  • 2021-01-02 09:26

    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.

    0 讨论(0)
提交回复
热议问题