-1

Is there a way to automatically get the date when the year and month are selected without clicking the done button?

$(function() {
  $('.date-picker, .to').datepicker({
    changeMonth: true,
    changeYear: true,
    showButtonPanel: true,
    dateFormat: 'yy-mm-dd',
    onClose: function(dateText, inst) {
      $(this).datepicker('setDate', new Date(inst.selectedYear, inst.selectedMonth, 1));
      $('#to').datepicker('setDate', new Date(inst.selectedYear, inst.selectedMonth + 1, 0));
    }
  });
});
.ui-datepicker-calendar {
  display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js" integrity="sha512-uto9mlQzrs59VwILcLiRYeLKPPbS/bT71da/OEBYEwcdNUk8jYIy+D176RYoop1Da+f9mvkYrmj5MCLZWEtQuA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<label for="startDate">Year/Month :</label>
<input name="startDate" id="startDate" class="date-picker" />
<input name="startDate" id="to" class="to" />

2
  • First thing is first, you need include the jquery script since you are using the jQuery selector $, and the jquery-ui script since you are using datepicker, a jQuery UI widget. CDNs for those scripts can be found here at cdnjs.com
    – BeerusDev
    Commented Aug 30, 2021 at 15:15
  • 1
    You mean like using the onChangeMonthYear event? Reading the documentation is a good habit to get into. Commented Aug 30, 2021 at 15:24

1 Answer 1

0

Consider the following example.

$(function() {
  function setDate(target, year, month, string) {
    var myDate;
    if ($(target).is("#first")) {
      day = 1;
      myDate = $.datepicker.parseDate("yy-mm-dd", year + "-" + month + "-01");
    } else {
      myDate = new Date(year, month, 0);
    }
    if (string) {
      $(target).val($.datepicker.formatDate("yy-mm-dd", myDate));
    } else {
      $(target).datepicker("setDate", myDate);
    }
  }

  $('.date-picker').datepicker({
    changeMonth: true,
    changeYear: true,
    showButtonPanel: true,
    dateFormat: 'yy-mm-dd',
    onChangeMonthYear: function(yy, mm) {
      setDate(this, yy, mm, true);
    },
    onClose: function(dateText, inst) {
      if ($(this).is("#first")) {
        setDate("#last", inst.selectedYear, inst.selectedMonth + 1, true);
      }
    }
  });
});
.ui-datepicker-calendar {
  display: none;
}
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<label for="startDate">Year/Month :</label>
<input id="first" class="date-picker" />
<input id="last" class="date-picker" />

If the User changes the Month and Year they want, yet then clicks away, this will populate the field, in the same you were doing in onClose.

2
  • thank you. Based on your answer, I have created the function I want. Additionally I would like to keep the second input box disabled by default, and remove the disable when a date is entered in the first input box. Is there any way?
    – leesuccess
    Commented Aug 31, 2021 at 3:10
  • @leesuccess yes.
    – Twisty
    Commented Aug 31, 2021 at 15:10

Not the answer you're looking for? Browse other questions tagged or ask your own question.