IE JavaScript date parsing error

后端 未结 6 1695
南方客
南方客 2020-11-28 15:02

Why cannot IE parse this string as a Date object.

var d = Date.parse(\"Fri Jun 11 04:55:12 +0000 2010\"); // returns NaN

However, it works

6条回答
  •  星月不相逢
    2020-11-28 15:49

    Problem

    In case your date is stored in SQL datetime like 2020-04-07 05:30:00 and want to parse it in IE. When you parse it with JavaScript in IE using new Date(), it outputs Invalid Date while latest versions of Chrome and Firefox parse this date correctly.

    Solution

    You have to replace with T in datetime string coming from SQL.

    Example

    let myDate = '2020-04-07 05:30:00';
    let myFormattedDate = myDate.replace(' ', 'T'); // '2020-04-07T05:30:00'
    console.log(new Date(myFormattedDate));
    

提交回复
热议问题