// 获取当前日期
const currentDate = new Date();
// 获取年、月、日
const year = currentDate.getFullYear();
const month = String(currentDate.getMonth() + 1).padStart(2, '0');
const day = String(currentDate.getDate()).padStart(2, '0');
// 格式化为 "yyyy-MM-dd" 格式
const formattedDate = `${year}-${month}-${day}`;
console.log(formattedDate); // 输出类似 "2023-08-31" 的格式化日期
JS中获取当前日期的yyyy-MM-dd格式日期
最新推荐文章于 2024-04-26 15:14:22 发布
本文展示了如何使用JavaScript的内置方法获取当前日期,然后将其格式化为yyyy-MM-dd格式。代码展示了如何提取年、月、日并正确填充0,以便得到一致的输出。
摘要由CSDN通过智能技术生成