JS时间戳转时间格式

    //转化为时间格式
    function getDate(timestamp) {
        timestamp = timestamp.replace("/Date(", "").replace(")/", "");
        if (timestamp.indexOf("+") > 0) {
            timestamp = timestamp.substring(0, timestamp.indexOf("+"));
        }
        else if (timestamp.indexOf("-") > 0) {
            timestamp = timestamp.substring(0, timestamp.indexOf("-"));
        }
        let date = new Date(parseInt(timestamp, 10));
        let year = date.getFullYear();
        let month = date.getMonth() + 1 < 10 ? /*"0" +*/ (date.getMonth() + 1) : date.getMonth() + 1;
        let currentDate =( date.getDate() < 10 ? /*"0" + */date.getDate() : date.getDate())+" ";
        let Hours = date.getHours() < 10 ? /*"0" + */date.getHours() : date.getHours();
        let Minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
        let Seconds = date.getSeconds() < 10 ? /*"0" +*/ date.getSeconds() : date.getSeconds();
        return `${year}年${month}月${currentDate}日${Hours}:${Minutes}:${Seconds}`;
    }

网上找了好多都不行。。。

posted @ 2019-02-23 23:13  小样儿哦  阅读(248)  评论(0编辑  收藏  举报