//原方法轉(zhuǎn)換自帶格式:xxxx-?xx月-xx?? ? ?xx?:?xx?:?xx
一,重寫(xiě)方法前:
例:time:1534301216395
調(diào)用:changeTime(time):
function changeTime(time){
?? ?var commonTime = "";
?? ?if(time){
?? ??? ?var unixTimestamp = new Date(time*1) ;
?? ??? ?commonTime = unixTimestamp.toLocaleString();
?? ?}
?? ? return commonTime;
}
結(jié)果:
?2018-8-15 10:46:56
//轉(zhuǎn)換為自己想要的格式,重寫(xiě) toLocaleString()。
二,重寫(xiě)方法后:
例:time:1534301216395
調(diào)用:changeTime(time):
Date.prototype.toLocaleString = function() {
??? return this.getFullYear() + "年" + (this.getMonth() + 1) + "月" + this.getDate() + "日 " + this.getHours() + "點(diǎn)" + this.getMinutes() + "分" + this.getSeconds() + "秒";
};
結(jié)果:
2018年8月15日 10點(diǎn)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);//拼接時(shí)間
? ? ? ? return oTime;
? ? }else{
? ? ? ? return "";
? ? }
}
//補(bǔ)0
function getBz(num){
? ? if(parseInt(num) < 10){
? ? ? ? num = '0'+num;
? ? }
? ? return num;
}
結(jié)果:
2018-08-15 10:46:56
————————————————
版權(quán)聲明:本文為CSDN博主「ZWH@隨筆」的原創(chuàng)文章,遵循 CC 4.0 BY-SA 版權(quán)協(xié)議,轉(zhuǎn)載請(qǐng)附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/sevenmt/article/details/81700759