[坑]IE11之Javascript Bug

前言

其實我也不知道這到底算不算Bug

更多的像是一種功能上的不完善或未定義

那就來看看這個問題怎麼重現吧

重現步驟

這次工作上遇到的情況是出現在 Javascript 的 Date 物件

<!DOCTYPE html>
<html>
<head>
	<title></title>
</head>
<body>
	<div id="view"></div>
	<script type="text/javascript">
		let s = new Date('2019-09-25 00:00:00').toISOString();
		document.getElementById('view').innerText = s;
	</script>
</body>
</html>

引發錯誤的部分是

2019-09-25 00:00:00

這樣子的日期格式,IE11不支援

錯誤內容為SCRIPT5001: 必須要有數字

必須把 - 取代成 /

2019/09/25 00:00:00

支援度

Chrome FireFox IE Edge
O O X O

補充

JavaScript.tw 社團中的社員指出,IE支援的格式為 ISO8601

也就是上方無法通過的格式,在日期與時間中間需加入一個T

2019-09-25T00:00:00

這樣也可以順利運作

另外文中也提到,ES5之前最好不要使用Date.parse

若有需要維護舊瀏覽器請注意這一塊

It is not recommended to use Date.parse as until ES5, parsing of strings was entirely implementation dependent. There are still many differences in how different hosts parse date strings, therefore date strings should be manually parsed (a library can help if many different formats are to be accommodated).