兼容IE解决[date] Invalid Date问题

发布时间 2023-04-28 14:37:11作者: 只有一

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));