当前位置 : 主页 > 网络编程 > JavaScript >

两种修改 js时间显示格式

来源:互联网 收集:自由互联 发布时间:2021-06-30
js 时间显示格式 //方法1 var a= (new Date()).toLocaleDateString();//获取当前日期 a = a.replace(/\//g,'-');//替换2017/05/03 为 2017-05-03 //console.log(a); //console.log(new Date()); //方法2 //var oldTime = Math.round(new D
js 时间显示格式
//方法1
    var a= (new Date()).toLocaleDateString();//获取当前日期
    a = a.replace(/\//g,'-');//替换2017/05/03 为    2017-05-03
    //console.log(a);
    //console.log(new Date());

    //方法2
    //var oldTime = Math.round(new Date().getTime())-86400000;
    var oldTime = Math.round(new Date().getTime());
    var d = new Date(oldTime);
    function add0(m) {
        return m < 10 ? '0' + m : m
    }
    function formatDate(now) {
        var year = now.getFullYear();
        var month = now.getMonth() + 1;
        var date = now.getDate();
        var hour = now.getHours();
        var minute = now.getMinutes();
        var second = now.getSeconds();
        //return year+"-"+add0(month)+"-"+add0(date)+" "+add0(hour)+":"+add0(minute)+":"+add0(second);
        return year + "-" + add0(month) + "-" + add0(date);
    }
    var preTime = formatDate(new Date());
    //var preTime = formatDate(d);
    //console.log(preTime);
网友评论