How To Get Current Month Last Day in JavaScript?


Share:
Published By: Admin, Published On: Aug 02, 2021, Category: Javascript
Hello Friends, Now let's see example of how to use javascript get current month last day example. We will use how to use if get current month last day in javascript. Here you will learn how to use javascript get current month last day. This is a short guide on get current month last day. Let's get started with how to get current month last day in javascript. Here i will give you many example how you can check javascript get current month last day. Example: 1
<!DOCTYPE html>
<html>
    <head>
        <title>JavaScript Get Last Day of a Given Month</title>
        <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
    </head>
    <body class="bg-dark">
        <div class="container mt-5">
            <div class="row">
                <div class="container">
                    <div class="col-md-6 offset-md-3">
                        <div class="card">
                            <div class="card-header">
                                <h5>JavaScript Get Last Day of a Given Month</h5>
                            </div>
                            <div class="card-body">
                                <label>Select a Date: </label>
                                <input type="date" id="lastDate" class="form-control" onmouseout="getTheDays()" />
                                <p class="mt-2"><label id="first_d">First Day of the Entered Month: </label></p>
                                <p><label id="last_d">Last Day of the Entered Month: </label></p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <script>
            function getTheDays() {
                var day = new Date(document.getElementById('lastDate').value);
                var month = day.getMonth(),
                    year = day.getFullYear();
                var FirstDay = new Date(year, month, 1);
                var LastDay = new Date(year, month + 1, 0);

                var weekday = new Array();
                weekday[0] = "Sunday";
                weekday[1] = "Monday";
                weekday[2] = "Tuesday";
                weekday[3] = "Wednesday";
                weekday[4] = "Thursday";
                weekday[5] = "Friday";
                weekday[6] = "Saturday";

                if (typeof weekday[FirstDay.getDay()] != 'undefined') {
                    document.getElementById('first_d').innerHTML = weekday[FirstDay.getDay()] +
                        ' (' + FirstDay.toDateString('dd/mon/yyyy') + ')';
                    document.getElementById('last_d').innerHTML = weekday[LastDay.getDay()] +
                        ' (' + LastDay.toDateString('dd/mon/yyyy') + ')'; ;
                }
                else {
                    document.getElementById('first_d').innerHTML = '';
                    document.getElementById('last_d').innerHTML = '';
                }
            }
        </script>
    </body>
</html>
Output:
Select a Date: 27/07/2019
Monday (Mon Jul 01 2019)
Wednesday (Wed Jul 31 2019)
Example: 2
<!DOCTYPE html>
<html>
    <head>
        <title>JavaScript Get Last Day of a Given Month</title>
        <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
    </head>
    <body class="bg-dark">
        <div class="container mt-5">
            <div class="row">
                <div class="container">
                    <div class="col-md-6 offset-md-3">
                        <div class="card">
                            <div class="card-header">
                                <h5>JavaScript Get Last Day of a Given Month</h5>
                            </div>
                            <div class="card-body">
                                <label>Select a Date: </label>
                                <input type="date" id="getDate" class="form-control" onmouseout="getTheDays()" />
                                <p class="mt-2"><label id="F_Day">First Day Result of Entered Month: </label></p>
                                <p><label id="L_Day">Last Day Result of Entered Month: </label></p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <script>
            function getTheDays() {
                var day = new Date(document.getElementById('getDate').value);
                var month = day.getMonth(),
                    year = day.getFullYear();
                var FirstDay = new Date(year, month, 1);
                var LastDay = new Date(year, month + 1, 0);

                var weekday = new Array();
                weekday[0] = "Sunday";
                weekday[1] = "Monday";
                weekday[2] = "Tuesday";
                weekday[3] = "Wednesday";
                weekday[4] = "Thursday";
                weekday[5] = "Friday";
                weekday[6] = "Saturday";

                if (typeof weekday[FirstDay.getDay()] != 'undefined') {
                    document.getElementById('F_Day').innerHTML = weekday[FirstDay.getDay()] +
                        ' (' + FirstDay.toDateString('dd/mon/yyyy') + ')';
                    document.getElementById('L_Day').innerHTML = weekday[LastDay.getDay()] +
                        ' (' + LastDay.toDateString('dd/mon/yyyy') + ')'; ;
                }
                else {
                    document.getElementById('F_Day').innerHTML = '';
                    document.getElementById('L_Day').innerHTML = '';
                }
            }
        </script>
    </body>
</html>
Output:
Select a Date: 12/12/2020
Tuesday (Tue Dec 01 2020)
Thursday (Thu Dec 31 2020)

It will help you....

Share: