用js將從後臺得到的時間戳轉換成日期

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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章