Get month from specific date using jquery

In this article we will learn how to get month from given specific date or current date or datepicker from jQuery.





Now a day’s common issue is that when anyone want to get month from any specific date or current date or any date selected control like calendar or datepicker using jQuery, it always returns current month minus one. It means if we want to get current month from today’s date it always returns last month not current month.

Example: Suppose today’s date is 2018-02-11. It represents 11th Feb 2018. But I want to get only month from this date then returns 01 not 02.  

Why this happen. I will clarify everything step by step.

Let’s start with practically step by step.

Open VisualStudio->File->New-> Project with GetMonth



After click on OK button below dialog box will appear.


After click on OK button our project is created successfully and look like this.


Now add a html page which is Index.html.





After click on OK button then Index.html page is created.

<!DOCTYPE html>
<html>
<head>
    <title></title>
       <meta charset="utf-8" />
</head>
<body>

</body>
</html>

Now I will define label control for bind the value.

<div style="margin-top:50px; margin-left:100px">
        <b>Date :</b>  <label id="lblDate">2018-02-11</label><br /><p>
        <label id="lblmonth"><b>Month :       </b></label>
        <label id="lblgetMonth"/>
</div>

Now come on our main issue. First, I will create an object of new date like.

$(document).ready(function () {
            var getCurrentDate = new Date($("#lblDate").text());
            $('#lblgetMonth').text(getCurrentDate.getMonth());
  });  


Output:


Now you can see on above output I have write date 11th Feb 2018. When we call getMonth() method from given date then 2nd months ( Feb ) should be show but here show 01( Jan ).
Why it happened because we are calculating Jan means 1, Feb means 2…. etc. Means we are calculating months from 1 to 12. But jQuery getMonth() method calculating it from 0 to 11. This means when we called Jan (01) then jQuery (0). This means if we want to get correct month from given any date. We have to need getMonth () +1.

Example: On the same specific date when we call getMonth() +1 then you can see then correct answer.Our jQuery method is that.

$(document).ready(function () {
            var getCurrentDate = new Date("2018-02-11");
            $('#lblgetMonth').text(getCurrentDate.getMonth()+1);
        });

Output:


Now when we change specific date from 2018-02-11 to 2018-12-18.
Then you can the output without Add one in getMonth() (Date.getmonth()).

<script type="text/javascript">
        $(document).ready(function () {
            var getCurrentDate = new Date($("#lblDate").text());
            $('#lblgetMonth').text(getCurrentDate.getMonth());
        });     
    </script>
<div style="margin-top:50px; margin-left:100px">
        <b>Date :</b>  <label id="lblDate">2018-12-18</label><br /><p>
        </p>
        <label id="lblmonth"><b>Month :       </b></label>
        <label id="lblgetMonth"/>
    </div>

 But same specific date we get month using getMonth()+1, then output.


<script type="text/javascript">
        $(document).ready(function () {
            var getCurrentDate = new Date($("#lblDate").text());
            $('#lblgetMonth').text(getCurrentDate.getMonth() + 1);
        });     

</script>

<div style="margin-top:50px; margin-left:100px">
        <b>Date :</b>  <label id="lblDate">2018-12-18</label><br /><p>
        </p>
        <label id="lblmonth"><b>Month :       </b></label>
        <label id="lblgetMonth"/>
 </div>



Conclusion:  In this article we have solved big issue which is get month from given specific date or any other date using jquery it shows always actual month from minus one. We are calculating month which start from 01 to 12 (Jan to Dec) but jQuery’s getMonth () always calculating months start from 0 to 11 (Jan to Dec). Due to this reason jQuery getMonth method always return last month from our specific date month. For solve this issue we call getMonth()+1.

No comments

Powered by Blogger.