ganesh kaspate
ganesh kaspate

Reputation: 2685

Jquery month and year picker not working

I am new to the web development. Here, I am using the jquery datePicker. In this I have a input box and that user can select only year and month and not date. So, I used,

$('#' + duration[k]).datepicker({
                      format: 'MM yyyy',
                      viewMode: "months",
                      minViewMode: "months",
                    });

I did this, Here my input are getting created dynamically.

var duration = ["StartDuration", "EndDuration"];
    var commentText = document.createElement('input');
                            commentText.setAttribute("type", "text");
                            commentText.name = "month";
                            commentText.id = duration[k];
                            commentText.className = 'form-control';
                            commentText.rows = '3';
                            commentText.placeholder = "Enter" + " " + duration[k];
                            commentText.setAttribute("readOnly", "true");
                            commentText.setAttribute("ng-model", duration[k]);

Used for loop on the array and then created this input box. Now what happening is here, It gets datepicker but it is having a date-month-year . So, I want to have only year and month. Can any one pleas help me with this ?

Upvotes: 0

Views: 1470

Answers (2)

Krina Soni
Krina Soni

Reputation: 930

You can do like this

$('.date-picker').datepicker( {
        changeMonth: true,
        changeYear: true,
        showButtonPanel: true,
        dateFormat: 'MM yy',
    });

Jquery datepicker provides these two - changeMonth,changeYear property for it and make format like MM yy.

Upvotes: 0

Emad Dehnavi
Emad Dehnavi

Reputation: 3441

You need to use dateformat like this : dateFormat: 'MM yy'

Check the example :

$(function() {
    $('.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));
        }
    });
});
.ui-datepicker-calendar {
  display: none;
}
<link href="//ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/themes/base/jquery-ui.css" rel="stylesheet"/>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js"></script>
<label for="startDate">Your Date :</label>
<input name="startDate" id="startDate" class="date-picker" />

Here is fiddle

Upvotes: 1

Related Questions