码迷,mamicode.com
首页 > Web开发 > 详细

JS时间戳转时间格式

时间:2019-02-24 00:56:34      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:func   replace   转化   lse   col   turn   string   function   格式   

    //转化为时间格式
    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}`;
    }

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

JS时间戳转时间格式

标签:func   replace   转化   lse   col   turn   string   function   格式   

原文地址:https://www.cnblogs.com/heheblog/p/10424830.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!