例如我要获取2011-09-06 15:16:25请问怎么获取的啊
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);
}
我在Date原型上扩展了这个方法。
https://github.com/meiseayoun...
使用方法 new Date().format("YYYY-MM-DD hh:mm:ss")
如果不想污染原型,请移步至http://momentjs.com/
使用方法 moment(new Date()).format("YYYY-MM-DD hh:mm:ss")
13 回答12.8k 阅读
7 回答1.9k 阅读
5 回答1.4k 阅读
3 回答2.6k 阅读✓ 已解决
3 回答1.1k 阅读✓ 已解决
5 回答1.1k 阅读✓ 已解决
2 回答1.2k 阅读✓ 已解决
new Date('2011-09-06 15:16:25').getTime()
时间戳