How to make Jquery datepicker accept only months and year?

Asked

Viewed 5,714 times

2

How can I convert my jQuery datepicker to accept only months and years ?

  • you are using this datepicker https://jqueryui.com/datepicker/ ?

  • 1

    Example using jQueryUI datepicker: http://jsfiddle.net/DBpJe/5106/

3 answers

3


For this jQuery datepicker you can make a hack that works the following way:

Add None to the CSS class of the days as below:

.ui-datepicker-calendar {
    display: none;
}

Soon after you set up your datepicker that way:

$('.date-picker').datepicker({
        changeMonth: true,
        changeYear: true,
        showButtonPanel: true,
        dateFormat: 'MM yy',
        onClose: function (dateText, inst) {
            var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val();
            var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val();
            $(this).datepicker('setDate', new Date(year, month, 1));
        }
    });

Follows the jsfiddle.

  • 1

    Good idea, the only problem I see is if there are other datepickers on the same page. But joining a id or a specific class is solved. +1

  • Well posted @Sergio, I’ll put this remark :))

  • 1

    Very good was exactly what I needed. Thank you

2

  • 1

    I did some tests here with this datepicker, besides it being very beautiful works beautifully rs .. + 1 :D

-1

<script>
$(".datepicker" ).datepicker({
  startView: 1,
  minViewMode: 1,
  maxViewMode: 3,
  format: "mm-yyyy",
  changeMonth: true,
  changeYear: true
});
</script>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.