How do I format a Microsoft JSON date?

后端 未结 30 3176
伪装坚强ぢ
伪装坚强ぢ 2020-11-21 04:48

I\'m taking my first crack at Ajax with jQuery. I\'m getting my data onto my page, but I\'m having some trouble with the JSON data that is returned for Date data types. Basi

30条回答
  •  北恋
    北恋 (楼主)
    2020-11-21 05:09

    I get the date like this:

    "/Date(1276290000000+0300)/"
    

    In some examples the date is in slightly different formats:

    "/Date(12762900000000300)/"
    "Date(1276290000000-0300)"
    

    etc.

    So I came up with the following RegExp:

    /\/+Date\(([\d+]+)\)\/+/
    

    and the final code is:

    var myDate = new Date(parseInt(jsonWcfDate.replace(/\/+Date\(([\d+-]+)\)\/+/, '$1')));
    

    Hope it helps.

    Update: I found this link from Microsoft: How do I Serialize Dates with JSON?

    This seems like the one we are all looking for.

提交回复
热议问题