//当月今天之前的日期
function getPastDatesInMonth() {
// 获取今天的日期
const today = new Date();
today.setHours(0, 0, 0, 0); // 设置时间为当天的午夜 00:00:00
// 获取当月的第一天
const firstDayOfMonth = new Date(today.getFullYear(), today.getMonth(), 1);
// 创建一个数组来存储今天之前的日期
const pastDates = [];
// 从第一天开始,直到今天之前的每一天
for (let date = new Date(firstDayOfMonth); date < today; date.setDate(date.getDate() + 1)) {
const formattedDate = `${date.getFullYear()}-${(date.getMonth() + 1).toString().padStart(2, '0')}-${date.getDate().toString().padStart(2, '0')}`;
pastDates.push(formattedDate);
}
console.log(pastDates)
return pastDates;
}
js 获取当月今天之前的日期
最新推荐文章于 2024-11-19 15:41:25 发布
本文介绍了如何使用JavaScript编写一个名为`getPastDatesInMonth`的函数,该函数获取给定月份内所有过去的日期,以字符串格式存储在数组中,以便于日期处理和分析。

9311

被折叠的 条评论
为什么被折叠?



