644 B
644 B
title, type, language, tags, cover, dateModified
| title | type | language | tags | cover | dateModified | |
|---|---|---|---|---|---|---|
| Number of days in month | snippet | javascript |
|
laptop-plants-2 | 2021-06-13T05:00:00-04:00 |
Gets the number of days in the given month of the specified year.
- Use the
Dateconstructor to create a date from the givenyearandmonth. - Set the days parameter to
0to get the last day of the previous month, as months are zero-indexed. - Use
Date.prototype.getDate()to return the number of days in the givenmonth.
const daysInMonth = (year, month) => new Date(year, month, 0).getDate();
daysInMonth(2020, 12)); // 31
daysInMonth(2024, 2)); // 29