1、Date
日期对象
var date = new Date();
var data_target = new Date('2023-1-1');
var year = date.getFullYear();
var month = date.getMonth() + 1;
var data_date = date.getDate();
var day = date.getDay();
var hours = date.getHours();
var minutes = date.getMinutes();
var seconds = date.getSeconds();
console.log("今天是:" + year + "年" + month + "月" + data_date + "日" + ",星期" + day);
console.log("时间为:" + hours + ":" + minutes + ":" + seconds);
2、setInterval
定时器方法,每一秒输出一次
var n = 0;
setInterval(function () {
console.log(n++);
}, 1000)
3、setInterval
定时器方法,获取动态当前时间(时钟)
setInterval(function () {
var date = new Date();
var hours = date.getHours();
var minutes = date.getMinutes();
var seconds = date.getSeconds();
console.log(hours + ":" + minutes + ":" + seconds);
}, 1000)