毫秒轉換日期


//原方法轉換自帶格式: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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM