第一种方法:
var time="2018-05-19T08:04:52.000+0000";
var d = new Date(time);
var times=d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate() + ' ' + d.getHours() + ':' + d.getMinutes() + ':' + d.getSeconds();
输出 2018-05-19 15:59:10
发现问题:
如果时间是09:00:00 ,你这样转之后显示的是9:0:0
2018-05-19T00:00:00.000+0000
转换后:"2018-5-19 8:0:0"
解决办法(网友提供):加个判断就可以了 例如月(d.getMonth() + 1 < 10 ? "0" + (d.getMonth() + 1) : d.getMonth() + 1)
eg:
dateTime(rowData){
// console.log(rowData)
var d = new Date(rowData)
// var a= d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate() + ' ' + d.getHours() + ':' + d.getMinutes() + ':' + d.getSeconds(); /*d.getMonth() + 1 < 10 ? "0" + (d.getMonth() + 1) : d.getMonth() + 1*/
var a= d.getFullYear() + '-' + (d.getMonth() + 1 < 10 ? "0" + (d.getMonth() + 1) : d.getMonth() + 1) + '-' + (d.getDate()<10 ? "0" +d.getDate():d.getDate())+ ' ' + (d.getHours()<10 ?"0"+d.getDate():d.getDate()) + ':' + (d.getMinutes()<10 ? "0" + d.getMinutes(): d.getMinutes()) + ':' + (d.getSeconds()<10 ? "0" + d.getSeconds() : d.getSeconds() );
// console.log(a)
return a
},
另一种办法:
myFunction();
function myFunction(){
var dateee = new Date("2018-05-19T00:00:00.000+0000").toJSON();
// var dateee = new Date("2017-07-09T09:46:49.667").toJSON();
var date = new Date(+new Date(dateee)+8*3600*1000).toISOString().replace(/T/g,' ').replace(/.[d]{3}Z/,'')
alert(date);
console.log("时间2==="+date);
}