
<body>
<p>原始日期:<br/>2017-10-19T14:51:52</p>
<p id="test"></p>
<p id="test2"></p>
<p id="test3"></p>
<script>
Date.prototype.Format = function(c) {
var o = {
"M+": this.getMonth() + 1,
"d+": this.getDate(),
"h+": this.getHours(),
"m+": this.getMinutes(),
"s+": this.getSeconds(),
"q+": Math.floor((this.getMonth() + 3) / 3),
S: this.getMilliseconds()
};
/(y+)/.test(c) && (c = c.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length)));
for (var a in o) new RegExp("(" + a + ")").test(c) && (c = c.replace(RegExp.$1, 1 == RegExp.$1.length ? o[a] : ("00" + o[a]).substr(("" + o[a]).length)));
return c
};
var t = new Date('2017-10-19T14:51:52');
var getDate = function(d) {
return "number" != typeof d && (d = 1e3 * parseInt(d)), d ? new Date(d).Format("yyyy-MM-dd hh:mm:ss") : "0000-00-00 00:00:00"
}
var t2 = getDate(t.getTime());
document.getElementById('test').innerHTML = '时间格式化t:<br/>'+t;
document.getElementById('test2').innerHTML = '时间格式化t2:<br/>'+t2;
document.getElementById('test3').innerHTML = '时间-当前系统时间:<br/>'+new Date();
</script>
</body>
原因不太清楚,从结果看new Date()的时候把传入的时间当成是世界标准时间了,可以明确的给时间后面加上时区:new Date('2017-10-19T14:51:52+0800');
补充
在safari浏览器,new Date() 中传入的参数的参数中不能识别‘-’,‘T’,所以需要转化一下,兼容性函数如下: