function getDaysInCurrentMonth() {
const currentDate = new Date(); // Get current date
const currentYear = currentDate.getFullYear(); // Get current year
const currentMonth = currentDate.getMonth(); // Get current month
const firstDayOfMonth = new Date(currentYear, currentMonth, 1); // First day of the month
const lastDayOfMonth = new Date(currentYear, currentMonth + 1, 0); // Last day of the month
const totalDays = lastDayOfMonth.getDate(); // Get number of days in the current month
const dateRange = [];
for (let day = 1; day <= totalDays; day++) {
const date = new Date(currentYear, currentMonth, day);
dateRange.push(date);
}
return dateRange;
}
const daysInCurrentMonth = getDaysInCurrentMonth();
console.log("Date range in the current month:", daysInCurrentMonth);
js获取当月的天数
最新推荐文章于 2024-07-29 11:53:27 发布