得到的時間戳: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