js將毫秒數轉化為時間


    // 根據毫秒數構建 Date 對象
    var date = new Date(1499996760000);
    // 格式化日期
    dateTime = date.toLocaleString();

 

 


這時候 dateTime 的值為"2018/07/10 下午2:07:02"。可以通過重寫 toLocaleString() 方法,來自定義日期顯示格式。

 

 

 

 


    // 重寫方法,自定義格式化日期
    Date.prototype.toLocaleString = function() {
        // 補0   例如 2018/7/10 14:7:2  補完后為 2018/07/10 14:07:02
        function addZero(num) {
            if(num<10)
                return "0" + num;
            return num;
        }
        // 按自定義拼接格式返回
        return this.getFullYear() + "/" + addZero(this.getMonth() + 1) + "/" + addZero(this.getDate()) + " " +
            addZero(this.getHours()) + ":" + addZero(this.getMinutes()) + ":" + addZero(this.getSeconds());
    };
    // 根據毫秒數構建 Date 對象
    var date = new Date(1499996760000);
    // 按重寫的自定義格式,格式化日期
    dateTime = date.toLocaleString();
---------------------
作者:cherlshall
來源:CSDN
原文:https://blog.csdn.net/cherlshall/article/details/80985209
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!


免責聲明!

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



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