js获取当前时间

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))

 

你可能感兴趣的:(js)