133

I am using bootstrap datepicker and my code is like following, Demo of the code on jsfiddle

<div class="input-append date" id="datepicker" data-date="02-2012" 
     data-date-format="mm-yyyy">

 <input  type="text" readonly="readonly" name="date" >    
 <span class="add-on"><i class="icon-th"></i></span>      
</div>      


$("#datepicker").datepicker({
        viewMode: 'years',
         format: 'mm-yyyy'
    });

The above code is working fine, but what I am trying to do is allow users pick months and years only.

Could you please tell me how to do that?

black_belt
  • 6,601
  • 36
  • 121
  • 185
  • I'd just use two select boxes... it would make a lot more sense than using a calendar widget, and trying to hide the actual calendar. –  Feb 20 '13 at 07:54
  • 8
    Would you still use two select boxes even if you had an option like this http://jsfiddle.net/Kz2sW/1/ ? :) – black_belt Feb 20 '13 at 08:06

9 Answers9

313

How about this :

$("#datepicker").datepicker( {
    format: "mm-yyyy",
    viewMode: "months", 
    minViewMode: "months"
});

Reference : Datepicker for Bootstrap


For version 1.2.0 and newer, viewMode has changed to startView, so use:

$("#datepicker").datepicker( {
    format: "mm-yyyy",
    startView: "months", 
    minViewMode: "months"
});

Also see the documentation.

Martin Tournoij
  • 26,737
  • 24
  • 105
  • 146
Iswanto San
  • 18,263
  • 13
  • 58
  • 79
8

Notice that in the version of 1.2.0 the viewMode has changed to startView.

eg:

$('#sandbox-container input').datepicker({
  startView: 1,
  minViewMode: 1
});

http://eternicode.github.io/bootstrap-datepicker/?markup=component&format=yyyy&weekStart=&startDate=&endDate=&startView=2&minViewMode=2&todayBtn=false&language=zh-CN&orientation=auto&autoclose=on&forceParse=on#sandbox

Liber
  • 422
  • 4
  • 12
4

I'm using version 2(supports both bootstrap v2 and v3) and for me this works:

$("#datepicker").datepicker( {
    format: "mm/yyyy",
    startView: "year", 
    minView: "year"
});
Jamie Rees
  • 7,973
  • 2
  • 45
  • 83
Tanya
  • 277
  • 3
  • 4
3

For the latest bootstrap-datepicker (1.4.0 at the time of writing), you need to use this:

$('#myDatepicker').datepicker({
    format: "mm/yyyy",
    startView: "year", 
    minViewMode: "months"
})

Source: Bootstrap Datepicker Options

Sheharyar
  • 73,588
  • 21
  • 168
  • 215
1

I am using bootstrap calender for future date not allow with allow change in months & year only..

var j = jQuery.noConflict();
        j(function () {
            j(".datepicker").datepicker({ dateFormat: "dd-M-yy" }).val()
        });

        j(function () {
            j(".Futuredatenotallowed").datepicker({
                changeMonth: true,
                maxDate: 0,
                changeYear: true,
                dateFormat: 'dd-M-yy',
                language: "tr"
            }).on('changeDate', function (ev) {
                $(this).blur();
                $(this).datepicker('hide');
            }).val()
        });
1

You should have to add only minViewMode: "months" in your datepicker function.

Nisarg Bhavsar
  • 946
  • 2
  • 18
  • 40
1

To view the months of the current year, and only the months, use this format - it only takes the months of that year. It can also be restricted so that a certain number of months is displayed.

<input class="form-control" id="txtDateMMyyyy"  autocomplete="off" required readonly/>

<script>
('#txtDateMMyyyy').datetimepicker({
    format: "mm/yyyy",
    startView: "year",
    minView: "year"
}).datetimepicker("setDate", new Date());
</script>
Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
  • 5
    Thank you for this code snippet, which might provide some limited, immediate help. A [proper explanation](https://meta.stackexchange.com/q/114762/349538) would greatly improve its long-term value by showing why this is a good solution to the problem and would make it more useful to future readers with other, similar questions. Please [edit] your answer to add some explanation, including the assumptions you’ve made. – Ismael Padilla Jan 22 '20 at 23:07
  • Se agrego una pequeña descripcion de lo que hace el codigo – Marco Puenayan Feb 06 '20 at 03:37
0

Hello if you want to get only year this will help you

  $('.date-own').datepicker({
     minViewMode: 2,
     format: 'yyyy'
   });
abubakkar tahir
  • 737
  • 1
  • 11
  • 13
-1

Why not call the $('.input-group.date').datepicker("remove"); when the select statement is changed then set your datepicker view then call the $('.input-group.date').datepicker("update");

Kiel
  • 483
  • 3
  • 5
  • 18