//原方法轉換自帶格式:xxxx-xx月-xx xx:xx:xx
一,重寫方法前:
例:time:1534301216395
調用:changeTime(time):
function changeTime(time){
var commonTime = "";
if(time){
var unixTimestamp = new Date(time*1) ;
commonTime = unixTimestamp.toLocaleString();
}
return commonTime;
}
結果:
2018-8-15 10:46:56
//轉換為自己想要的格式,重寫 toLocaleString()。
二,重寫方法后:
例:time:1534301216395
調用:changeTime(time):
Date.prototype.toLocaleString = function() {
return this.getFullYear() + "年" + (this.getMonth() + 1) + "月" + this.getDate() + "日 " + this.getHours() + "點" + this.getMinutes() + "分" + this.getSeconds() + "秒";
};
結果:
2018年8月15日 10點46分56秒
三,第三種方法:
例:time:1534301216395
function changeTime(time){
if(time){
var oDate = new Date(time*1),
oYear = oDate.getFullYear(),
oMonth = oDate.getMonth()+1,
oDay = oDate.getDate(),
oHour = oDate.getHours(),
oMin = oDate.getMinutes(),
oSen = oDate.getSeconds(),
oTime = oYear +'-'+ getBz(oMonth) +'-'+ getBz(oDay) +' '+ getBz(oHour) +':'+ getBz(oMin) +':'+getBz(oSen);//拼接時間
return oTime;
}else{
return "";
}
}
//補0
function getBz(num){
if(parseInt(num) < 10){
num = '0'+num;
}
return num;
}
結果:
2018-08-15 10:46:56
原文:https://blog.csdn.net/sevenmt/article/details/81700759