4

I am using datepicker to select year-month only. I filter out the day part like so:

$(el).datepicker( 
{changeMonth: true, 
changeYear: true, 
dateFormat:'yymm' } 
); 

The code comes from the dataInit option in jqGrid. When the user clicks in a day only the year-month is passed back to the input box.

Is there any way to just show months where the days are?

Thanks.

wattostudios
  • 8,666
  • 13
  • 43
  • 57
Fabio
  • 41
  • 1
  • 1
  • 2

3 Answers3

8

This code is working flawlessly for me:

$(function()
{   
    $(".monthPicker").datepicker({
        dateFormat: 'MM yy',
        changeMonth: true,
        changeYear: true,
        showButtonPanel: true,

        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).val($.datepicker.formatDate('MM yy', new Date(year, month, 1)));
        }
    });

    $(".monthPicker").focus(function () {
        $(".ui-datepicker-calendar").hide();
        $("#ui-datepicker-div").position({
            my: "center top",
            at: "center bottom",
            of: $(this)
        });
    });
});

<label for="month">Month: </label>
<input type="text" id="month" name="month" class="monthPicker" />
Craig O
  • 842
  • 5
  • 14
Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
0

Well, if you're not tied to the datepicker plugin, you could use one of the "monthpickers" that are available, like this one: http://plugins.jquery.com/project/monthpicker.

(Edit) there's also a previous discussion here: In search of JavaScript Month Picker.

Community
  • 1
  • 1
Spiny Norman
  • 8,277
  • 1
  • 30
  • 55
  • Thanks for the link to the discussion. I guess it is what I expected, there is no month picker functionality in datepicker. The other project does not have the same look and feel that I need on my current project. I guess I am going to have to look at adding this functionality myself. – Fabio Jan 18 '11 at 19:43
0

the tweaks over jQuery api tend to break on new releases. They're not working for me anymore, so I wrote a jQuery widget for that. Please check it out: http://lucianocosta.info/jquery.mtz.monthpicker

Luciano Costa
  • 3,492
  • 2
  • 16
  • 8