转载 https://blog.youkuaiyun.com/qq_38052210/article/details/80277569
得到的时间戳:1526007949000;
处理:
var unixTimestamp = new Date(1526007949000);
commonTime = unixTimestamp.toLocaleString();
console.log(commonTime)
结果:2018/5/11 上午11:05:49
如果希望转换成自己需要的格式,需要从写一下toLocalString()方法;
Date.prototype.toLocaleString = function() {
return this.getFullYear() + "/" + (this.getMonth() + 1) + "/" + this.getDate() +" "+ this.getHours() + ":" + this.getMinutes() + ":" + this.getSeconds();
};
结果:2018/5/11 11:5:49
Date.prototype.toLocaleString = function() {
return this.getFullYear() + "年" + (this.getMonth() + 1) + "月" + this.getDate() +"日"+" "+ this.getHours() + "时" + this.getMinutes() + "分" + this.getSeconds()+"秒";
};
结果:2018年5月11日 11时5分49秒
日期转换成时间戳
var date = new Date('2018-05-11 11:05:49');
console.log(date.getTime())
结果:1526007949000