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

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));
posted @ 2023-04-28 14:33  DurianTRY  阅读(43)  评论(0编辑  收藏  举报