Open In App

How to get the first and last date of current month using JavaScript ?

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Given a month and the task is to determine the first and last day of that month in proper format using JavaScript.

JavaScript getDate() Method: This method returns the day of the month (from 1 to 31) for the defined date. 

Syntax:

Date.getDate()

Parameters: This method does not accept any parameters.

Return value: It returns a number from 1 to 31, representing the day of the month.

JavaScript getFullYear() Method: This method returns the year (four digits for dates between the year 1000 and 9999) of the defined date. 

Syntax:

Date.getFullYear()

Parameters: This method does not accept any parameters.

Return value: It returns a number, representing the year of the defined date.

JavaScript getMonth() Method: This method returns the month (from 0 to 11) for the defined date, based on to local time. 

Syntax:

Date.getMonth()

Parameters: This method does not accept any parameters.

Return value: It returns a number, from 0 to 11, representing the month.

Example 1: This example sets the date of the var firstDay and lastDay by getting the year and month of the current date using getFullYear() and getMonth() method and then getting the first and last day of the month. 

Javascript




function GFG_Fun() {
    let date = new Date();
    let firstDay = new Date(date.getFullYear(), date.getMonth(), 1);
    let lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);
    console.log("First day=" + firstDay)
    console.log("Last day = " + lastDay);
}
GFG_Fun()


Output

First day=Thu Jun 01 2023 00:00:00 GMT+0000 (Coordinated Universal Time)
Last day = Fri Jun 30 2023 00:00:00 GMT+0000 (Coordinated Universal Time)

Example 2: This example is similar to the previous one. This example also sets the date of the var firstDay and lastDay by getting the year and month of the current date using getFullYear() and getMonth() method and then get the first and last day of the month by a different approach. 

Javascript




function daysInMonth(month, year) {
    return new Date(year, month, 0).getDate();
}
 
function GFG_Fun() {
    let date = new Date();
    let firstDay = new Date(date.getFullYear(),
        date.getMonth(), 1);
 
    let lastDay = new Date(date.getFullYear(),
        date.getMonth(), daysInMonth(date.getMonth() + 1,
            date.getFullYear()));
 
    console.log("First day = " + firstDay)
    console.log("Last day = " + lastDay);
}
GFG_Fun()


Output

First day = Thu Jun 01 2023 00:00:00 GMT+0000 (Coordinated Universal Time)
Last day = Fri Jun 30 2023 00:00:00 GMT+0000 (Coordinated Universal Time)

Using Moment.js library: Javascript library called Moment.js is used. Due to this, parsing, modifying, validation, and displaying dates and times in javascript.

Syntax:

const moment = require('moment');
const date = new Date('2022/01/01');
moment(date).format('MMMM d, YYYY');

Example: In this example with the help of Moment.js we get the first day and last day date of the current month.

HTML




<html>
 
<head>
    <script src=
    </script>
    <script
        src=
    </script>
</head>
<body>
    <h1 style="color:green;">
        GeeksForGeeks
    </h1>
    <p>Click on button to get the first and
        last day of current month
    </p>
    <p id="firstDate"></p>
    <p id="lastdate"></p>
    <button onClick="data()">
        click here
    </button>
    <script>
        function data() {
            const firstdate = moment().startOf('month').format('DD-MM-YYYY');
            const lastdate = moment().endOf('month').format("DD-MM-YYYY");
            document.getElementById("firstDate").innerHTML = "First day = " + firstdate;
            document.getElementById("lastdate").innerHTML = "Last day = " + lastdate;
        }
 
    </script>
</body>
</html>


Output:

 



Last Updated : 05 Jun, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads