HOWTO · JavaScript
How to Get First and Last Day of Month Using JavaScript
This tutorial shows how to get the last and first day of the month using methods in JavaScript.
On this page
This JavaScript tutorial tackles how to get the first and last day of a month. Many projects need to find the first and last date of a month — for reports, billing periods, and date pickers.
Two Date methods do the groundwork: getFullYear() tells us the year, and getMonth() tells us the month. With those two values, the Date constructor can build the boundary dates directly.
Use the Date.getFullYear() Method in JavaScript
The Date.getFullYear() method returns the year of the specified date according to local time. Note that the example builds the date with the Date(year, monthIndex, day) constructor; the month index is zero-based, so 0 is January.
const randomDate = new Date(2018, 0, 20);
console.log(randomDate.getFullYear());
Output:
2018
Use the Date.getMonth() Method in JavaScript
The getMonth() method returns the month of the specified date as a zero-based value: 0 indicates the first month of the year (January) and 11 the last (December).
const randomDate = new Date(2018, 0, 20);
console.log(randomDate.getMonth());
Output:
0
We must remember that a date’s month number is zero-indexed in JavaScript. So January will be month 0 and December month 11.
Get the First and Last Day of a Month Using JavaScript
Two constructor calls produce the boundary dates:
- The first day is
new Date(year, month, 1)— day1of the same month. - The last day is
new Date(year, month + 1, 0)— day0of the next month. Day0rolls back one day from the first of the next month, landing exactly on the last day of the month we want, whatever its length.
The following function packages both, and the example applies it to January 2018:
function getMonthRange(date) {
const firstDay = new Date(date.getFullYear(), date.getMonth(), 1);
const lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);
return { firstDay, lastDay };
}
const january2018 = getMonthRange(new Date(2018, 0, 20));
console.log(january2018.firstDay.toDateString());
console.log(january2018.lastDay.toDateString());
Output:
Mon Jan 01 2018
Wed Jan 31 2018
To get the first and last day of the current month, pass the current date: getMonthRange(new Date()). The same function works for any month — you never need to look up how many days a month has, because the day-0 roll-back handles 28-, 29-, 30- and 31-day months alike. February of the leap year 2024 shows this:
const february2024 = getMonthRange(new Date(2024, 1, 15));
console.log(february2024.firstDay.toDateString());
console.log(february2024.lastDay.toDateString());
Output:
Thu Feb 01 2024
Thu Feb 29 2024
Conclusion
The first day of a month is new Date(year, month, 1), and the last day is new Date(year, month + 1, 0) — the day before the first of the next month. Feeding the year and zero-based month from getFullYear() and getMonth() into these two constructor calls gives the first and last day of the current month or of any particular month, leap years included.