JavaScript Date:计算月份的最后一天并处理闰年
要获取本月最后一天的日号,可以用 new Date(year, monthIndex + 1, 0).getDate()。从同一个 Date 对象中用 getFullYear() 和 getMonth() 读取 year 和 monthIndex。计算自动处理闰年的二月和十二月之后的跨年情况。
为什么下个月第 0 天就是本月最后一天
在 new Date(year, monthIndex, day) 中,一月的月份索引是 0,十二月是 11。把 day 设为 0 会从指定月份的第一天往前退一天,因此 2024 年三月的第 0 天就是二月 29 日。计算十二月最后一天时,月份索引 12 会进入下一年一月。 ECMAScript · Date Constructor
const last = new Date(2024, 2, 0);
console.log(last.getFullYear(), last.getMonth() + 1, last.getDate()); // 2024 2 29
计算本月的最后一天
只读取一次当前时刻,然后从同一个对象中取出年和月。这样即使正好遇到换月,两个值也来自同一时刻。代码按运行环境的本地时区确定当前月份,getDate() 返回 28 到 31 中的一个数。如果要取 UTC 当前月份,请把 now.getUTCFullYear() 和 now.getUTCMonth() + 1 传给下面的函数。
const now = new Date();
const year = now.getFullYear();
const monthIndex = now.getMonth();
const lastDay = new Date(year, monthIndex + 1, 0).getDate();
console.log(lastDay);
根据输入的年和月计算
该函数接受 1 到 9999 年和 1 到 12 月。把月份数值原样传入,UTC 日期中的月份索引就指向下个月。例如,输入二月使用数值 2,而索引 2 是三月,所以第 0 天是二月最后一天。使用多个数字参数的 new Date() 会将 0 到 99 年映射到 1900 到 1999 年,setUTCFullYear() 则保留指定的年份。用 getUTCDate() 读取结果可以避免运行环境时区的影响。 ECMAScript · Date.prototype.setUTCFullYear()
function lastDayOfMonth(year, month1to12) {
if (!Number.isInteger(year) || year < 1 || year > 9999 ||
!Number.isInteger(month1to12) || month1to12 < 1 || month1to12 > 12) {
throw new RangeError();
}
const date = new Date(0);
date.setUTCFullYear(year, month1to12, 0);
return date.getUTCDate();
}
console.log(lastDayOfMonth(2024, 2)); // 29
console.log(lastDayOfMonth(2023, 2)); // 28
console.log(lastDayOfMonth(2024, 12)); // 31
console.log(lastDayOfMonth(96, 2)); // 29
能被 4 整除的年份是闰年,其中能被 100 整除的年份还必须能被 400 整除。因此 1900 年二月有 28 天,2000 年二月有 29 天,0096 年二月也有 29 天。Date 已经应用了这条规则。 ECMAScript · Day Number and Time within Day
运行完整示例
将代码保存为 last-day.html,然后在浏览器中打开。页面起初显示本地时区的当前年、月及当月最后一天。输入年份 2024 和月份 2,点击计算,结果是 2024-02-29。2023 年二月得到 2023-02-28,2024 年十二月得到 2024-12-31。如果月份填 0 或 13,浏览器会提示超出范围。
<!doctype html>
<html lang="zh">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>计算月份的最后一天</title>
</head>
<body>
<h1>计算月份的最后一天</h1>
<form id="month-form">
<label>年 <input id="year" name="year" type="number" min="1" max="9999" step="1" required></label>
<label>月 <input id="month" name="month" type="number" min="1" max="12" step="1" required></label>
<button type="submit">计算</button>
</form>
<p>最后一天 <output id="result" for="year month"></output></p>
<script>
function lastDayOfMonth(year, month1to12) {
if (!Number.isInteger(year) || year < 1 || year > 9999 ||
!Number.isInteger(month1to12) || month1to12 < 1 || month1to12 > 12) {
throw new RangeError();
}
const date = new Date(0);
date.setUTCFullYear(year, month1to12, 0);
return date.getUTCDate();
}
const form = document.querySelector('#month-form');
const yearInput = form.elements.namedItem('year');
const monthInput = form.elements.namedItem('month');
const result = document.querySelector('#result');
const now = new Date();
function showLastDay() {
const year = yearInput.valueAsNumber;
const month = monthInput.valueAsNumber;
const day = lastDayOfMonth(year, month);
const pad = (value, length) => String(value).padStart(length, '0');
result.textContent = [pad(year, 4), pad(month, 2), pad(day, 2)].join('-');
}
yearInput.value = now.getFullYear();
monthInput.value = now.getMonth() + 1;
if (form.checkValidity()) showLastDay();
form.addEventListener('submit', event => {
event.preventDefault();
if (form.reportValidity()) showLastDay();
});
</script>
</body>
</html>




