JavaScript Date: Get the Last Day of a Month and Handle Leap Years

Get the day number for the end of this month with new Date(year, monthIndex + 1, 0).getDate(). Read year and monthIndex from the same Date object using getFullYear() and getMonth(). The calculation accounts for leap-year February and the change of year after December.

Why day zero of next month gives the last day

In new Date(year, monthIndex, day), January has index 0 and December has index 11. A day of 0 goes back one day from the first of the specified month. Day zero of March 2024 is therefore February 29. For December, the calculation uses month index 12, which rolls over to January of the next year. ECMAScript · Date Constructor

const last = new Date(2024, 2, 0);
console.log(last.getFullYear(), last.getMonth() + 1, last.getDate()); // 2024 2 29

Get the last day of the current month

Read the current time once and take the year and month from the same object so they refer to the same instant at a month boundary. This code selects the current month in the runtime’s local time zone. getDate() returns a number from 28 to 31. To select the current month in UTC, pass now.getUTCFullYear() and now.getUTCMonth() + 1 to the function below.

const now = new Date();
const year = now.getFullYear();
const monthIndex = now.getMonth();
const lastDay = new Date(year, monthIndex + 1, 0).getDate();
console.log(lastDay);

Calculate from a year and month

This function accepts years from 1 to 9999 and months from 1 to 12. Passing the month unchanged selects the following month on the Date object in UTC. For instance, a February input of 2 is the index for March, so day zero gives the end of February. The multi-argument new Date() constructor remaps years 0–99 to 1900–1999, while setUTCFullYear() uses the year as supplied. getUTCDate() reads the result independently of the runtime’s time zone. 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

A leap year is divisible by 4, except that years divisible by 100 must also be divisible by 400. February therefore has 28 days in 1900 and 29 in 2000. February of year 0096 also has 29 days. Date already applies this calendar rule. ECMAScript · Day Number and Time within Day

Try the complete example

Save the code as last-day.html and open it in a browser. It starts with the current year, month, and last day in the local time zone. Enter year 2024 and month 2, then select Calculate. The result is 2024-02-29. February 2023 gives 2023-02-28, and December 2024 gives 2024-12-31. If you enter month 0 or 13, the browser reports an out-of-range value.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Find the last day of a month</title>
</head>
<body>
<h1>Find the last day of a month</h1>
<form id="month-form">
  <label>Year <input id="year" name="year" type="number" min="1" max="9999" step="1" required></label>
  <label>Month <input id="month" name="month" type="number" min="1" max="12" step="1" required></label>
  <button type="submit">Calculate</button>
</form>
<p>Last day <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>