Get the First and Last Day of the Current Month in JavaScript

To get the first and last day of the current month, we can use the built-in Date() constructor with the getFullYear() and the getMonth() methods.

The Date() constructor returns a date object based on the parameters passed to it. If no parameters are passed, it returns the current date time osbject.

But if you want to get a specific day’s date object, you can pass the following three parameters to it:

  1. The year
  2. The month
  3. The day of the month

It means if we want to get the first day of the current month, we have to pass the current year, current month, and the day as 1 to the Date() constructor.

To get the current year, we can use the getFullYear() method. The getFullYear() method returns the year of the specified date. Similarly, to get the current month, we can use the getMonth() method.

The following example shows how to get the first day of the current month:

// Get current date object
let date = new Date();

let firstDayDate = new Date(date.getFullYear(), date.getMonth(), 1);

console.log(firstDayDate);
// Output: Tue Nov 01 2022 00:00:00 GMT+0530 (India Standard Time)

// Extract first day name from first day date
let firstDay = firstDayDate.toString().split(' ')[0];

console.log(firstDay);
// Output: Tue

To get the last day of the current month, we have to understand the functionality of the third parameter of the Date() constructor.

The third parameter of the Date() constructor, specifies the day of the month. For example, if we pass it as 1 i.e. Date(year, month, 1), it will give us the first day’s date of the given month. Similarly, Date(year, month, 2) will give the second day’s date of the given month.

But, if we set the day parameter to 0(zero), the Date() constructor returns the last day of the previous month. It means if we set the day parameter of the next month to 0(zero), the Date() constructor will return us the last day of the current month.

The following example shows how we can get the last day of the current month:

// Get current date object
let date = new Date();

// Get last day of the current month
let lastDayDate = new Date(date.getFullYear(), date.getMonth() + 1, 0);

console.log(lastDayDate);
// Output: Wed Nov 30 2022 00:00:00 GMT+0530 (India Standard Time)

// Extract last day name from last day date
let lastDay = lastDayDate.toString().split(' ')[0];

console.log(lastDay);
// Output: "Wed"

Note that the month index starts at 0. That is, 0 represents January month, 1 represents February and so on up to 11(December).


Get the First and Last Day of a Given Month

You can also use the above code to get the first and last day of a given month.

But keep in mind that the month index in JavaScript starts at 0. So, if you want to get the first and last day of January month, you have to pass month as 0, similarly for December, you have to pass 11.

The following example shows how we can get the first and last day of a given month:

function getFirstDay(year, month){
    let firstDay = new Date(year, month, 1);
    return firstDay;
}

function getLastDay(year, month){
    let lastDay = new Date(year, month + 1, 0);
    return lastDay;
}

console.log(getFirstDay(2022, 0));  // Sat Jan 01 2022 ...
console.log(getLastDay(2022, 0));   // Mon Jan 31 2022 ...

console.log(getFirstDay(2022, 5));  // Wed Jun 01 2022 ...
console.log(getLastDay(2022, 5));   // Thu Jun 30 2022 ...

Author

  • Manoj Kumar

    Hi, My name is Manoj Kumar. I am a full-stack developer with a passion for creating robust and efficient web applications. I have hands-on experience with a diverse set of technologies, including but not limited to HTML, CSS, JavaScript, TypeScript, Angular, Node.js, Express, React, and MongoDB.