時間戳轉(zhuǎn)為日期格式

在線時間戳轉(zhuǎn)換工具
在java中獲取時間戳方式:

//獲取現(xiàn)在的秒級別的時間戳
long time = System.currentTimeMillis() / 1000;
//獲取現(xiàn)在的毫秒級別的時間戳
long time = System.currentTimeMillis();

下面方法中,在調(diào)用Date構(gòu)造函數(shù)而不傳遞參數(shù)的情況下,新創(chuàng)建的對象自動獲得當(dāng)前日期和時間。如果想傳入特定日期,需將表示日期的字符串傳遞給Date構(gòu)造函數(shù)。

//時間戳轉(zhuǎn)為時間格式y(tǒng)yyy-MM-dd HH:mm:ss
    function getDateTimeToDate(a) {
        //這里接收的是一個String類型的數(shù)值
        var now;
        //當(dāng)時間戳小于1000000000000時代表是秒級別的時間戳
        if (parseInt(a) < 1000000000000) {
            now = new Date(parseInt(a) * 1000);
        } else {
            now = new Date(parseInt(a));
        }
        var year = now.getFullYear() < 10 ? '0' + now.getFullYear() : now.getFullYear();  //取得4位數(shù)的年份
        var month = now.getMonth() + 1 < 10 ? '0' + (now.getMonth() + 1) : now.getMonth() + 1;  //取得日期中的月份,其中0表示1月,11表示12月
        var date = now.getDate() < 10 ? '0' + now.getDate() : now.getDate();      //返回日期月份中的天數(shù)(1到31)
        var hour = now.getHours() < 10 ? '0' + now.getHours() : now.getHours();    //返回日期中的小時數(shù)(0到23)
        var minute = now.getMinutes() < 10 ? '0' + now.getMinutes() : now.getMinutes();//返回日期中的分鐘數(shù)(0到59)
        var second = now.getSeconds() < 10 ? '0' + now.getSeconds() : now.getSeconds();//返回日期中的秒數(shù)(0到59)
        return year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second;
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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

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