js如何获取时间

例如我要获取2011-09-06 15:16:25请问怎么获取的啊

阅读 3k
5 个回答

new Date('2011-09-06 15:16:25').getTime()

时间戳

新手上路,请多包涵

var date = new Date();
var year = date.getFullYear();
var month = date.getMonth()+1;
var day = date.getDate();
var hour = date.getHours();
var minute = date.getMinutes();
var second = date.getSeconds();
console.log(year+'-'+month+'-'+day+'-'+hour+':'+minute+':'+second);

如果格式是这样的 记得把位数补足

function getTime() {
            var date = new Date();
            var month = date.getMonth() + 1;
            var strDate = date.getDate();
            if (month >= 1 && month <= 9) {
                month = "0" + month;
            }
            if (strDate >= 0 && strDate <= 9) {
                strDate = "0" + strDate;
            }
            var Time = date.getFullYear() + "-" + month + "-" + strDate +
                " " + (date.getHours() <= 9 ? ("0" + date.getHours()) : date.getHours()) +
                ":" + (date.getMinutes() <= 9 ? ("0" + date.getMinutes()) : date.getMinutes()) +
                ":" + (date.getSeconds() <= 9 ? ("0" + date.getSeconds()) : date.getSeconds());
            console.log(Time);
        }
推荐问题