将月份映射到该月的最后一天的数量

时间:2022-04-03 20:15:35

How would I map the month number to the number of the last day of that month, taking into consideration for February leap year the last day is 28 else it's 29

我如何将月份数字映射到该月最后一天的数字,考虑到2月闰年,最后一天是28,否则它是29

1 个解决方案

#1


0  

Sure, you can get the last day of the month like this:

当然,你可以像这样得到一个月的最后一天:

var month = 1; // Feb
var date = new Date((new Date()).getYear(), month + 1, 0);
alert("Last day of the month is: " + date.getDate());

Here it is as a function:

这是一个功能:

function getLastDayOfMonth(month) {
   return (new Date((new Date()).getYear(), month + 1, 0)).getDate();
}

#1


0  

Sure, you can get the last day of the month like this:

当然,你可以像这样得到一个月的最后一天:

var month = 1; // Feb
var date = new Date((new Date()).getYear(), month + 1, 0);
alert("Last day of the month is: " + date.getDate());

Here it is as a function:

这是一个功能:

function getLastDayOfMonth(month) {
   return (new Date((new Date()).getYear(), month + 1, 0)).getDate();
}