原生Js获取年月日时分秒
代码中的now,即是当前时间。
var now = getNow();
function getNow() {
var d = new Date();
var year = d.getFullYear();
var month = change(d.getMonth() + 1);
var day = change(d.getDate());
var hour = change(d.getHours());
var minute = change(d.getMinutes());
var second = change(d.getSeconds());
function change(t) {
if (t < 10) {
return "0" + t;
} else {
return t;
}
}
var time = year + '年' + month + '月' + day + '日 ' + hour + '时' + minute + '分' + second + '秒';
return time;
}