How To Get First And Last Day Of The Current Month In JavaScript

get first and last day of the current month in JavaScript

Get first and last day of the current month in JavaScript is not complicated. Just using getFullYear() and getMonth() methods can solve the above problem. Follow this tutorial to understand these methods better and use them in your program.

Get first and last day of the current month in JavaScript

Method 1: Get the first day of the current month 

We will use the getFullYear() and getMonth() methods to do this. The above two methods allow us to get the current year and month on your machine, from which we can get the first day of the current month.

Syntax:

Date.getFullYear()
Date.getMonth()

Notes: getMonth() returns the value 0-11, equivalent to January to December.

Example:

var toDay = new Date();
console.log(toDay);
var firstDayOfMonth = new Date(toDay.getFullYear(), toDay.getMonth(), 1);
console.log(firstDayOfMonth);

Output:

Tue Sep 27 2022
Thu Sep 01 2022 

I used the constructor without parameters in this example to create a new date. This constructor returns the value of the Date and time that coincides with my creation time (Tue Sep 27 2022), then uses getFullYear() and getMonth() to create a new date whose return value is the first day of the current month with the constructor being.

What if we don’t want to get the first day of the current month, but we want to get the first Date or any of the previous month? I can help you follow the example below:

var toDay = new Date();
console.log(toDay);
var firstDayOfMonth = new Date(toDay.getFullYear(), toDay.getMonth() - 1, 1);
console.log(firstDayOfMonth);

Output:

Tue Sep 27 2022 
Mon Aug 01 2022

In the example, you see I use the (-) operator to get the value of the front door price from which to know the first day of the previous month.

Method 2: Last day of the current month

Similar to getting the first day of the current month, we also use the getFullYear() and getMonth() methods to get the last day of the current month. Still, we may not know what day is the last day of the current month, so we don’t. You can pass a value to create a date because that might not be right to solve. We will pass the Date in the constructor as 0. And this is how I do it:

Example:

var toDay = new Date();
console.log(toDay);
var lastDayOfMonth = new Date(toDay.getFullYear(), toDay.getMonth(), 0);
console.log(lastDayOfMonth);

Output:

Tue Sep 27 2022 
Fri Aug 30 2022

In the example, we see the program has output wrong because when we pass 0 into the program, it will understand that I want to get the last day of the previous month. If we do not process it, the program will output us on the last day of the previous month. Current month, I would handle the following:

var toDay = new Date();
console.log(toDay);
var lastDayOfMonth = new Date(toDay.getFullYear(), toDay.getMonth() +1, 0);
console.log(lastDayOfMonth);

Output:

Tue Sep 27 2022 
Fri Aug 30 2022

I added one when we passed the number of months into the parameter. Then the program will output the exact Date we need.

Summary

Through this tutorial, I showed you how to get first and last day of the current month in JavaScript so that you won’t have to deal with this problem again. It would be best if you practised it. It will help you remember it longer. I hope the article helps you and your program.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *