Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert jQuery date picker to select only month and year?

How to convert jQuery date picker to select month and year only?. I tried it using date format, it working but show dates too. I am trying a way to select month and year only

I wrote code,

<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css">
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">
<script>
   $(function() {
       $( "#datepicker" ).datepicker({dateFormat: 'MM yy'});
   });
</script>
<input type="text" id="datepicker">
like image 420
Mak Avatar asked Dec 27 '13 05:12

Mak


People also ask

How do you only show month in date picker?

This can be done by setting "display:none" to ". ui-datepicker-calendar" CSS class.

How do I use Datepicker?

The datepicker is tied to a standard form input field. Focus on the input (click, or use the tab key) to open an interactive calendar in a small overlay. Choose a date, click elsewhere on the page (blur the input), or hit the Esc key to close. If a date is chosen, feedback is shown as the input's value.


2 Answers

Your answer is here.

http://jsfiddle.net/bopperben/DBpJe/

$(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));
    }
});
});

Credits to nitin from his post... jQuery UI DatePicker to show month year only

like image 174
Mitesh Vora Avatar answered Oct 22 '22 00:10

Mitesh Vora


This is what I did for create a dropdown instead of use datepicker. Just jQuery is needed.

HTML:

 <select class="form-control" id="yearMonthInput"></select>

javascript code:

var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];

    for (i = new Date().getFullYear() ; i > 2008; i--) {
        $.each(months, function (index, value) {
            $('#yearMonthInput').append($('<option />').val(index + "_" + i).html(value + " " + i));
        });                
    }

And looks in this way:

enter image description here

like image 14
Daniel Silva Avatar answered Oct 22 '22 00:10

Daniel Silva