134

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?

2
  • 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.
    – user684934
    Feb 20, 2013 at 7:54
  • 8
    Would you still use two select boxes even if you had an option like this jsfiddle.net/Kz2sW/1 ? :)
    – black_belt
    Feb 20, 2013 at 8:06

9 Answers 9

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.

5
  • 1
    Thanks but I had to replace format: " mm" with format: "mm-yyyy" . Its working now :)
    – black_belt
    Feb 20, 2013 at 8:03
  • 1
    It doesn't show the - between months and years if you keep a space before mm-yyyy in format: " mm-yyyy",, could you please edit your answer again?
    – black_belt
    Feb 20, 2013 at 8:08
  • Perfect. Thank you very much.
    – Tigin
    Jul 4, 2017 at 13:04
  • I don't think viewMode is a valid option as per the documentation. But the other options allowed the required display to work. Jun 25, 2018 at 5:56
  • Is there any option for autoclose? Nov 19, 2018 at 6:43
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

0
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"
});
1
  • change minView to minViewMode May 25, 2017 at 21:40
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

1
  • on click of month in bootstrap, it is showing the date and then entering text in mm/yy format Aug 30, 2019 at 13:28
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.

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>
2
  • 5
    Thank you for this code snippet, which might provide some limited, immediate help. A proper explanation 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. Jan 22, 2020 at 23:07
  • Se agrego una pequeña descripcion de lo que hace el codigo Feb 6, 2020 at 3:37
0

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

  $('.date-own').datepicker({
     minViewMode: 2,
     format: 'yyyy'
   });
-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");

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.