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

时间:2016-08-03 21:43:19

标签: javascript

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

1 个答案:

答案 0 :(得分:0)

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

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

这是一个功能:

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