How to Get the First and Last Day of a Month in JavaScript
When building calendars, reports, or date pickers, you frequently need to find the first and last day of a specific month. JavaScript's Date object provides a powerful and surprisingly simple way to do this by leveraging its "date rollover" feature.
This guide will teach you how to create two simple, reusable functions to get the first and last day of any given month. You will also learn how to apply these functions to get the dates for the current, previous, or next month.
The Core Method: Using the Date Constructor
The key to solving this problem is the new Date(year, monthIndex, day) constructor. It's smart enough to automatically calculate the correct date even when you provide "out-of-range" values for the day or month.
The Two Critical Rules:
- The
monthIndexis zero-based (0 = January, 1 = February, ..., 11 = December). - Passing
1as thedaygives you the first day of the specified month. - Passing
0as thedayis a clever trick: it gives you the last day of the previous month.
Goal 1: Getting the First Day of a Month
To get the first day of any month, you simply create a new Date with that month's index and 1 for the day.
For example, we want to find the first day of a given month and year.
So, we can defined a Reusable Function (Recommended Solution!):
/**
* Gets the first day of the month for a given year and month.
* @param {number} year The full year.
* @param {number} month The zero-based month index (0-11).
* @returns {Date} The first day of the month.
*/
function getFirstDayOfMonth(year, month) {
return new Date(year, month, 1);
}
// Example Usage:
// Get the first day of October (month index 9) 2023
const firstDay = getFirstDayOfMonth(2023, 9);
console.log(firstDay.toDateString()); // Output: "Sun Oct 01 2023"
Goal 2: Getting the Last Day of a Month
To get the last day of a month, we use the "day zero" trick. We ask for the "0th" day of the next month, and JavaScript automatically rolls back to the last day of the month we actually want.
For example, we want to find the last day of a given month and year.
So, we can defined a Reusable Function (Recommended Solution!):
/**
* Gets the last day of the month for a given year and month.
* @param {number} year The full year.
* @param {number} month The zero-based month index (0-11).
* @returns {Date} The last day of the month.
*/
function getLastDayOfMonth(year, month) {
// To get the last day, we go to the next month (month + 1) and ask for day 0.
return new Date(year, month + 1, 0);
}
// Example Usage:
// Get the last day of October (month index 9) 2025
const lastDay = getLastDayOfMonth(2025, 9);
console.log(lastDay.toDateString()); // Output: "Fri Oct 31 2025"
// This works perfectly for leap years too!
const lastDayOfFeb2024 = getLastDayOfMonth(2024, 1); // February is month 1
console.log(lastDayOfFeb2024.toDateString()); // Output: "Thu Feb 29 2024"
Practical Examples: Current, Previous, and Next Months
With these two utility functions defined above, handling any relative month is simple.
Get the first and last day of the CURRENT month
const today = new Date();
const currentYear = today.getFullYear();
const currentMonth = today.getMonth(); // 0-11
// --- Get the first and last day of the CURRENT month ---
const firstDayCurrent = getFirstDayOfMonth(currentYear, currentMonth);
const lastDayCurrent = getLastDayOfMonth(currentYear, currentMonth);
console.log('Current Month First Day:', firstDayCurrent.toDateString());
console.log('Current Month Last Day:', lastDayCurrent.toDateString());
Output:
Current Month First Day: Wed Oct 01 2025
Current Month Last Day: Fri Oct 31 2025
Get the first and last day of the PREVIOUS month
const today = new Date();
const currentYear = today.getFullYear();
const currentMonth = today.getMonth(); // 0-11
// --- Get the first and last day of the PREVIOUS month ---
// The Date constructor automatically handles rolling back the year if currentMonth is 0 (January)
const firstDayPrevious = getFirstDayOfMonth(currentYear, currentMonth - 1);
const lastDayPrevious = getLastDayOfMonth(currentYear, currentMonth - 1);
console.log('Previous Month First Day:', firstDayPrevious.toDateString());
console.log('Previous Month Last Day:', lastDayPrevious.toDateString());
Output:
Previous Month First Day: Mon Sep 01 2025
Previous Month Last Day: Tue Sep 30 2025
Get the first and last day of the NEXT month
const today = new Date();
const currentYear = today.getFullYear();
const currentMonth = today.getMonth(); // 0-11
// --- Get the first and last day of the NEXT month ---
const firstDayNext = getFirstDayOfMonth(currentYear, currentMonth + 1);
const lastDayNext = getLastDayOfMonth(currentYear, currentMonth + 1);
console.log('Next Month First Day:', firstDayNext.toDateString());
console.log('Next Month Last Day:', lastDayNext.toDateString());
Output:
Next Month First Day: Sat Nov 01 2025
Next Month Last Day: Sun Nov 30 2025
Conclusion
Finding the first and last days of a month is a straightforward task in JavaScript if you leverage the intelligent behavior of the Date constructor.
- To get the first day of a month, use
new Date(year, month, 1). - To get the last day of a month, use the clever "day zero" trick:
new Date(year, month + 1, 0). - Always remember that the month index is zero-based.
By encapsulating this logic in reusable functions, you can write clean, readable, and bug-free code for any date-range calculations.