1

Does anyone know why there is a difference in output month between the dateText month and the datepicker object selectedMonth, is it normal that January is 0?

https://jsfiddle.net/s6k398gr/

$(document).ready(function() {

 $('#datepicker').datepicker({
    dateFormat: 'dd-mm-yy',
    onSelect: function(dateText, e) {
        $('.selectedDate').text(dateText);
        $('.objectDate').text(e.selectedDay + '-' + e.selectedMonth + '-' + e.selectedYear);
    }
   });
 });
Chris. P.
  • 53
  • 1
  • 4
  • Possible duplicate of [why does javascript getMonth count from 0 and getDate count from 1?](http://stackoverflow.com/questions/15799514/why-does-javascript-getmonth-count-from-0-and-getdate-count-from-1) – Yohanes Gultom Mar 01 '17 at 00:05

1 Answers1

1

The getMonth() or e.selectedMonth method returns the month in the specified date according to local time, as a zero-based value (where zero indicates the first month of the year).

An integer number, between 0 and 11, representing the month in the given date according to local time. 0 corresponds to January, 1 to February, and so on.

$(document).ready(function(){
 $('#datepicker').datepicker({
   dateFormat: 'dd-mm-yy',
   onSelect: function(dateText, e){
     $('.selectedDate').text(dateText);
      $('.objectDate').text(e.selectedDay + '-' + ( e.selectedMonth +1) + '-' + e.selectedYear);
    }
  });
});
<link href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
<div id="datepicker">
</div>
<br>
<div>
  Selected Date: <span class="selectedDate"></span><br>
  Object Date: <span class="objectDate"></span>
</div>

JS fiddle demo : https://jsfiddle.net/geogeorge/c91jfzn7/

Geo George
  • 349
  • 2
  • 8