js日期转换

1. 转换为:2020-7-9

var d = new Date()

    function date(date) {
        var nowdate = new Date(date).toLocaleDateString().replace(/\//g, '-')
        return nowdate
    }
    console.log(date(d));

2. 转换为2020/7/9

var d = new Date()

    function date(date) {
        var nowdate = new Date(date).toLocaleDateString()
        return nowdate
    }
    console.log(date(d));

3. 转换为2020-07-09 11:30:23

var d = new Date()

    function date(now) {
        // var now = new Date(),
        y = now.getFullYear(),
            m = ("0" + (now.getMonth() + 1)).slice(-2),
            d = ("0" + now.getDate()).slice(-2);
        return y + "-" + m + "-" + d + " " + now.toTimeString().substr(0, 8);
    }
    console.log(date(d));

 

posted @ 2020-07-09 11:33  啦咯  阅读(214)  评论(0编辑  收藏  举报