時間戳轉換爲日期,網上搜了好幾個或多或少都有點問題,本身整理了一下,寫了個方法spa
console.log(formatDate(1565280000000))
輸出:
2019-08-09 00:00:00prototype
Date.prototype.format =function(datetime) { var date = new Date(datetime);//時間戳爲10位需*1000,時間戳爲13位的話不需乘1000 var year = date.getFullYear(), month = ("0" + (date.getMonth() + 1)).slice(-2), sdate = ("0" + date.getDate()).slice(-2), hour = ("0" + date.getHours()).slice(-2), minute = ("0" + date.getMinutes()).slice(-2), second = ("0" + date.getSeconds()).slice(-2); // 拼接 var result = year + "-"+ month +"-"+ sdate +" "+ hour +":"+ minute +":" + second; // 返回 return result; }
formatDate(datetime) { var date = new Date(datetime);//時間戳爲10位需*1000,時間戳爲13位的話不需乘1000 var year = date.getFullYear(), month = ("0" + (date.getMonth() + 1)).slice(-2), sdate = ("0" + date.getDate()).slice(-2), hour = ("0" + date.getHours()).slice(-2), minute = ("0" + date.getMinutes()).slice(-2), second = ("0" + date.getSeconds()).slice(-2); // 拼接 var result = year + "-"+ month +"-"+ sdate +" "+ hour +":"+ minute +":" + second; // 返回 return result; }