常用時(shí)間處理函數(shù)(持續(xù)更新中)

常用時(shí)間處理函數(shù)

一、時(shí)間戳轉(zhuǎn)化為時(shí)間

      function timestampToTime(timestamp) {
        var date = new Date(timestamp); 
        var Y = date.getFullYear() + '-';
        var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
        var D = date.getDate() + ' ';
        D = D < 10 ? '0' + D : D
        var h = date.getHours();
        h = h < 10 ? '0' + h : h
        var m = date.getMinutes();
        m = m < 10 ? '0' + m : m
        var s = date.getSeconds();
        s = s < 10 ? '0' + s : s
        return Y + M + D + h + ':' + m + ':' + s;
      }

二、根據(jù)時(shí)間撮,計(jì)算起止時(shí)間的時(shí)間間隔

      function calculateTimeDifference(timestampB, timestampA) {
        let timeDifference = timestampB - timestampA;
        let leave1 = timeDifference % (24 * 3600 * 1000);
        let days = Math.floor(timeDifference / (24 * 3600 * 1000));
        let hours = Math.floor(leave1 / (3600 * 1000));
        let leave2 = leave1 % (3600 * 1000);
        let minutes = Math.floor(leave2 / (60 * 1000));
        let leave3 = leave2 % (60 * 1000);
        let seconds = Math.round(leave3 / 1000);
        return days + "天" + hours + "小時(shí)" + minutes + "分鐘" + seconds + "秒";
      }

二、溫馨提示

在使用以上函數(shù)時(shí),一定要注意事時(shí)間戳是幾位數(shù)的。時(shí)間戳為10位需*1000,時(shí)間戳為13位不需乘1000;

最后編輯于
?著作權(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)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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