function getDaysInMonth(year, month) {
// 接受俩个参数 年,月
var date = new Date(year, month, 1);return new Date(date.getTime() - 864e5).getDate();
}
getDaysInMonth(2018, 2); // 28天
本文介绍了一个简单的JavaScript函数,该函数接受年份和月份作为参数,并返回该月份的总天数。通过创建一个Date对象并利用其内置的方法,此函数能够准确地计算出包括闰年情况在内的任何给定月份的天数。
function getDaysInMonth(year, month) {
// 接受俩个参数 年,月
var date = new Date(year, month, 1);}
getDaysInMonth(2018, 2); // 28天

被折叠的 条评论
为什么被折叠?