用js將時(shí)間毫秒數(shù)轉(zhuǎn)換為指定日期時(shí)間格式

//原方法轉(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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容