js获取当前时间

75 阅读1分钟

[本文已参与「新人创作礼」活动,一起开启掘金创作之路。]

var startDate = new Date();
function getCurrentTime (CurrentTime){
    var now = new Date(CurrentTime),
        y = now.getFullYear(),
        m = ("0" + (now.getMonth()+1)).slice(-2),
        d = ("0"+now.getDate()).slice(-2),
        //hh = ("0"+now.getHours()).slice(-2),
        //mm = ("0"+now.getMinutes()).slice(-2),
        //ss = ("0"+now.getSeconds()).slice(-2),;
        //return y + m + d + hh + mm + ss   //第一种
        return y + "-" + m + "-" + d + " " + now.toTimeString().substr(0,8)//第二种
}
console.log(getCurrentTime(startDate))