JS 根据月获取当月的所有天

11 min read
function getAllDaysInMonth(month, year) {
  const daysInMonth = getDaysInMonth(month, year);
  const allDays = [];
  for (let i = 1; i <= daysInMonth; i++) {
    allDays.push(new Date(year, month - 1, i));
  }
  return allDays;
}

console.log(getAllDaysInMonth(2, 2021)); 
// [
//   Mon Feb 01 2021 00:00:00 GMT+0000 (Coordinated Universal Time),
//   Tue Feb 02 2021 00:00:00 GMT+0000 (Coordinated Universal Time),
//   ...
//   Mon Feb 28 2021 00:00:00 GMT+0000 (Coordinated Universal Time)
// ]