560 B
560 B
title, tags
| title | tags |
|---|---|
| getMonthDays | date,intermediate |
Get the number of days in the month according to the specified month.
- Use
new Date(year, monthIndex, day), set the parameter day to0to get last day of the previous month. - Use
Date.prototype.getDateto get the day of month.
const getMonthDays = (year, month) => {
const today = new Date();
return new Date(year || today.getFullYear(), month || (today.getMonth() + 1), 0).getDate();
};
getMonthDays(); // 30
getMonthDays(2020, 2); // 29
getMonthDays(2021, 2); // 28