new Date() 在 ie 瀏覽器中兼容性的問題

問題描述javascript

function(value, row, index) {
	if (typeof(value)=="undefined"){
		return null;
	}
	return new Date(value).format("yyyy-MM-dd");
};

value傳入的值爲 「2017-12-26 00:00:00」java

在 chrome 和 firefox 中均能正常顯示chrome

在 ie 中則顯示爲「NaN-aN-aN」瀏覽器

問題緣由:spa

日期格式的字符串主要分爲兩種格式「yyyy-MM-dd」和「yyyy/MM/dd」,ie 不支持將第一種字符串格式直接轉換爲日期, 而第二種格式幾乎被全部的瀏覽器支持(包括 ie),因此在字符串轉換成日期前須要先對字符串的格式進行修改firefox

解決辦法:code

將字符串中全部的「-」都替換爲「/」orm

function(value, row, index) {
	if (typeof(value)=="undefined"){
		return null;
	}
	return new Date(Date.parse(value.replace(/-/g,"/"))).format("yyyy-MM-dd");
};
相關文章
相關標籤/搜索