• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / Using JavaScript to Get the Last Day of the Month

Using JavaScript to Get the Last Day of the Month

April 10, 2022 Leave a Comment

In JavaScript, we can get the last day of the month with the help of the new Date() constructor and come JavaScript code.

var currDate = new Date();
var currMonth = currDate.getMonth();
var currYear = currDate.getFullYear();
var newDate = new Date(currYear, currMonth + 1, 0);
var lastDayOfMonth = newDate.toString();

We first will get the current date using the new Date() object. This will give us the current day, month, and year. We then can use the getMonth() method to get the current month that it is.

We will also get the current year using the getFullYear() method and use this in the next part.

We then use the Date object again but this time to construct a new Date. We want the new date to be the last day of the month. To do this, we will enter the current year as the first parameter, followed by the current month. Since we want to get the last day of the month, what we will have to do is put in the next month, and then for the parameter for the day, we will enter in 0 for that value and it will give us the last day of the previous month.

So as you can see in the code above, we have entered the year, followed by the next month, followed by 0, which will return the last day of the previous month.

//var newDate = new Date(Year,Month,Day);
var newDate = new Date(currYear, currMonth + 1, 0);

We finally use the toString() method so that the date displays in a much friendlier format.

Using JavaScript to Get and Display the Last Day of the Month

Below we will provide code to get the last day of the month in JavaScript, and let the user see it in a friendly format when they click a button.

Here is our HTML set up:

<div id="div1">
  <div id="click-me" onclick="genLastDay()">Get Last Day</div>
  <p>The last day of the month is: <span id="theDate"></span></p>
</div>

We will first get the current date using new Date(). Once we have the date object, we can then get the day using the getDate() method, the year using the getFullYear() method, and the month using the getMonth() method.

We will then follow the code example at the top of this page to finish getting the day and display it in a friendly format.

Finally, we will show the day to the user by using the textContent property.

function genLastDay(){
  var currDate = new Date();
  var currMonth = currDate.getMonth();
  var currYear = currDate.getFullYear();
  var newDate = new Date(currYear, currMonth + 1, 0);
  var lastDayOfMonth = newDate.toString();
  document.getElementById("theDate").textContent = lastDayOfMonth;
}

The final code and output for this example is below:

Code Output:

Get Last Day

The last day of the month is:

Full Code:

<div id="div1">
  <div id="click-me" onclick="genLastDay()">Get Last Day</div>
  <p>The last day of the month is: <span id="theDate"></span></p>
</div>

<script>

function genLastDay(){
  var currDate = new Date();
  var currMonth = currDate.getMonth();
  var currYear = currDate.getFullYear();
  var newDate = new Date(currYear, currMonth + 1, 0);
  var lastDayOfMonth = newDate.toString();
  document.getElementById("theDate").textContent = lastDayOfMonth;
}

</script>

Hopefully this article has been useful in helping you understand how to use JavaScript to get the last day of the month.

Other Articles You'll Also Like:

  • 1.  Remove the First and Last Character From a String in JavaScript
  • 2.  setInterval JavaScript – How to Repeat Code in Set Time Intervals
  • 3.  JavaScript Check If Number is a Whole Number
  • 4.  Add Commas to Number in JavaScript
  • 5.  How to Change an Image on Hover Using JavaScript
  • 6.  Using JavaScript to Convert a String to Uppercase
  • 7.  Using JavaScript to Round to 3 Decimal Places
  • 8.  Creating a JavaScript Function to Subtract Two Numbers
  • 9.  Using JavaScript to Add Item to Array if it Does Not Exist
  • 10.  Using JavaScript to Rotate an Image

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy