How to Get First and Last Day of Month Using JavaScript

Tahseen Tauseef Feb 02, 2024
  1. Use the Date.getFullYear() Method in JavaScript to Get First and Last Day of Month
  2. Use the Date.getFullMonth() Method in JavaScript
  3. Get Last Day of the Month Using JavaScript
  4. Get the Particular Month’s First and Last Days Using JavaScript
How to Get First and Last Day of Month Using JavaScript

This JavaScript tutorial tackles how to get the first and last day of the current month. For many projects, we need to find out the first and last date of the month.

On getting the first and last day of the current month in JavaScript, we first need to learn how to use the getFullYear() and getFullMonth() methods.

Use the Date.getFullYear() Method in JavaScript to Get First and Last Day of Month

The Date.getFullYear() method returns output for the year of the specified date according to the local time.

const randomDate = new Date('Jan 20, 2018 00:12:18');
console.log(randomDate.getFullYear());

Output:

Use Date.getFullYear Method in JavaScript

Use the Date.getFullMonth() Method in JavaScript

For the month, we will use the Date.getMonth() method and add 1 to the result to get the date for the next month. The getMonth() method returns the month as a zero-based value (0 indicates the year’s first month).

const randomDate = new Date('Jan 20, 2018 00:12:18');
console.log(randomDate.getMonth());

Output:

Use Date.getFullMonth Method in JavaScript

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 Last Day of the Month Using JavaScript

We create a Date() object using the Date() constructor. The parameters will be the current year, current month + 1, and 0 for the day.

In addition, the Date object will contain the last day of the month. The three parameters we will be using for the Date().

var my_date = new Date();
var first_date = new Date(my_date.getFullYear(), my_date.getMonth(), 1);
document.write(first_date);
var last_date = new Date(my_date.getFullYear(), my_date.getMonth() + 1, 0);
document.write('<br>' + last_date);

Output:

Get last day of the month using JavaScript

Above we can see that the code gives us the first and last date of the current month, but this code is only for the current date. If we want to manually input the date, we can pass our custom date as a parameter inside the Date() constructor.

Get the Particular Month’s First and Last Days Using JavaScript

Now suppose that we want to get the first and last day of a particular month. For example, let’s assume that we need to find the first day and the last day of January 2018.

Let’s look at the example below.

var my_date = new Date('2018, Jan');
var first_date = new Date(my_date.getFullYear(), my_date.getMonth(), 1);
document.write(first_date);
var last_date = new Date(my_date.getFullYear(), my_date.getMonth() + 1, 0);
document.write('<br>' + last_date);

Output:

Get the Month&rsquo;s First and Last Days

This is balanced out because we passed 0 as the day parameter to the Date() constructor. So specifying a day of 0 means - giving us the last day of the month.

What happens is that we go one month forward by adding 1 to the return value from the getMonth method, and then we go one day back, to the last day of the month, by specifying 0 as the day.

Through this article, we first learned how to get the first and last day of the current month, and then we learned how to get the first and last day of a particular month using the Date.getFullYear() and Date.getMonth() method in JavaScript.

Related Article - JavaScript Date