get month start date and end date in JavaScript

get month start date and end date in JavaScript

Hello everyone, I am going to share the code sample to get start date and end date of current month using JavaScript. All the detail as given below.


The JavaScript code

<script>
    var nowdate = new Date();
    var monthStartDay = new Date(nowdate.getFullYear(), nowdate.getMonth(), 1);
    var monthEndDay = new Date(nowdate.getFullYear(), nowdate.getMonth() + 1, 0);

    $(function () {
        $("#startDate").html(monthStartDay);
        $("#endDate").html(monthEndDay);
    });
</script>

The output look like this.


The Live demo code sample to get the month start date and end date in JavaScript as given below.

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
    <script>
        var nowdate = new Date();
        var monthStartDay = new Date(nowdate.getFullYear(), nowdate.getMonth(), 1);
        var monthEndDay = new Date(nowdate.getFullYear(), nowdate.getMonth() + 1, 0);

        $(function () {
            $("#startDate").html(monthStartDay);
            $("#endDate").html(monthEndDay);
        });
    </script>
</head>
<body>
    <h1>get month start date and end date in JavaScript</h1>
    <div>
        Month StartDate : <label id="startDate" class="color"></label>
    </div>
    <div>
        Month EndDate : <label id="endDate" class="color"></label>
    </div>
</body>
</html>

The output : go to link http://embed.plnkr.co/tO4vTm/preview


Thank you!
ANIL SINGH

Anil Singh is an author, tech blogger, and software programmer. Book writing, tech blogging is something do extra and Anil love doing it. For more detail, kindly refer to this link..

My Tech Blog - https://www.code-sample.com/
My Books - Book 1 and Book 2

www.code-sample.com/. Powered by Blogger.
^