To check if a date is the last day of the month in JavaScript, you can use the getDate(), setDate(), and getTime() methods. Let’s get started.
How to check if a date is the last day of the month in JavaScript
To check if a date is the last day of the month in JavaScript, we will add one day to that date. If that date becomes the first day (the 1st day) of the next month, then that day is the last day of the month.
Example:
// Check if a date is the last day of the month function lastDay(date) { // Add one day to the input date object date.setDate(date.getDate() + 1); // Check if the day of the newly created date is equal to 1 return date.getDate() === 1; } const date1 = new Date('2023-01-30'); console.log(lastDay(date1)); const date2 = new Date('2022-01-31'); console.log(lastDay(date2));
Output:
false
true
We use the getDate()
method to get the day of the month of the input date object.
Then we use the setDate()
method with the input date object and set the setDate()
method’s parameter to the day of the month just received plus 1.
The setDate()
method will change the day of the month of the input date object to the value provided as a parameter.
If the object returned by the setDate()
method has a day of the month as 1, the input date is the last day of the month.
However, this solution will directly change the date used for the check. If you want to keep your input date object the same, refer to the following solution.
First, we will use the getTime()
method to get the number of milliseconds since January 1, 1970, 00:00:00 of the input date object.
We then add the number of milliseconds of a day to the value returned by the getTime()
method.
Using the new Date()
constructor with the total number of milliseconds just obtained will return us a date object representing the next date of the input date object.
Example:
// Check if a date is the last day of the month function lastDay(date) { // Add one day to the input date object const newDate = new Date(date.getTime() + 24 * 60 * 60 * 1000); // Check if the day of the newly created date is equal to 1 return newDate.getDate() === 1; } const date1 = new Date('2023-01-30'); console.log(lastDay(date1)); const date2 = new Date('2022-01-31'); console.log(lastDay(date2));
Output:
false
true
If the newly created date object has a day of the month of 1, the program will return true, meaning the input date is the last day of the month.
Click here to learn about the syntax and usage of the methods used in the article.
Summary
This article shared two solutions to check if a date is the last day of the month in JavaScript. If you want to keep your input date object the same, we recommend using the second solution for your program. Thank you for reading.

Hello, my name’s Bruce Warren. You can call me Bruce. I’m interested in programming languages, so I am here to share my knowledge of programming languages with you, especially knowledge of C, C++, Java, JS, PHP.
Name of the university: KMA
Major: ATTT
Programming Languages: C, C++, Java, JS, PHP