js时间格式和数字格式的相互转化

复制代码
//把日期格式转换成数字形式
    function changeTime(currentTime) {
        // currentTime 代表2021-07-16 09:00:00 传参
        if (currentTime.indexOf(':') < 0) {
            currentTime = currentTime + " 00:00:00";
        }
        let newTime = currentTime
        let reg = new RegExp("-", "g")  // 去掉时间里面的 -
        let a = newTime.replace(reg, "")
        let regs = new RegExp(" ", "g") // 去掉时间里面的空格
        let b = a.replace(regs, "")
        let regss = new RegExp(":", "g")  // 去掉时间里面的冒号
        let c = b.replace(regss, "")
        return c;
    }

//将数字转换成日期形式
    function changeTimeFormat(d) {
        var date = d.toString();
        return date.substring(0, 4) + "-" + date.substring(4, 6) + "-" + date.substring(6, 8) + " " + date.substring(8, 10) + ":" + date.substring(10, 12) + ":" + date.substring(12, 14);
    }
复制代码

 

posted @   RookieCoderAdu  阅读(2008)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示