可以使用 new Date()
来获取当前日期。例如:
lettoday = new Date();
console.log(today); // 输出当前日期
也可以使用 getFullYear()
、getMonth()
和 getDate()
方法分别获取年、月和日。例如:
lettoday = new Date();
let year = today.getFullYear();
let month = today.getMonth() + 1; // getMonth() 返回的月份是从 0 开始的
let date = today.getDate();
console.log(year + '-' + month + '-' + date); // 输出格式为 "YYYY-MM-DD" 的日期
还有很多其他用于获取时间信息的方法, 比如 getHours()
、getMinutes()
和 getSeconds()
。具体可以查看 JavaScript 的 Date 对象文档。