'Bootstrap Datetimepicker - Disable decades view mode

When the user clicks on the view header multiple times the expected behaviour is: days -> months -> years -> decades. I need to disable the decades view (screenshot attached). Doing so the user cannot go further 'year' view mode.

enter image description here



Solution 1:[1]

here is a workaround, thanks to RomeroMsk for this.

CSS:

.datepicker-years .picker-switch {
cursor: default !important;
background: inherit !important;
}

JS:

$('#datetimepicker1').datetimepicker({
// your datetimepicker options go here, f.i.:
        inline: true,
        sideBySide: false,
        format : "DD/MMM/YYYY",
        maxDate : moment()
}).on('dp.show dp.update', function () {
$(".datepicker-years .picker-switch").removeAttr('title')
    //.css('cursor', 'default')  <-- this is not needed if the CSS above is used
    //.css('background', 'inherit')  <-- this is not needed if the CSS above is used
    .on('click', function (e) {
        e.stopPropagation();
    });
});

Solution 2:[2]

After years of trying to get a workaround for this issue myself, I've finally cracked it.

I have adapted the answer from @Sarpe so that it disables to decades button on load. Sarpe's workaround required the user to click a date before disabling.

$('#datetimepicker1').datetimepicker({
  inline: true,
  sideBySide: false,
  format: 'YYYY-MM-DD',
  maxDate: moment()
});

$(".datepicker-years .picker-switch").addClass('disable').on('click', function(e) {
  e.stopPropagation();
});
.bootstrap-datetimepicker-widget .datepicker-years thead .picker-switch {
  cursor: default !important;
  background: inherit !important;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/eonasdan-bootstrap-datetimepicker/4.17.47/css/bootstrap-datetimepicker.min.css" rel="stylesheet"/>

<div id="datetimepicker1"></div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.9.0/moment-with-locales.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/eonasdan-bootstrap-datetimepicker/4.17.47/js/bootstrap-datetimepicker.min.js"></script>

Solution 3:[3]

You can do that by making changes to bootstrap-datetimepicker.js as well, this is the simplest way of doing this. All you have to do is open bootstrap-datetimepicker.js in editor -

  1. Find and comment the line -

    yearsViewHeader.eq(1).attr('title', options.tooltips.selectDecade);
    
  2. Just below the line "yearsView.find('.disabled').removeClass('disabled'); " add this line

    yearsViewHeader.eq(1).addClass('disabled');
    

You can find more elaborated steps at this link

Solution 4:[4]

This one is the best and simple solution.

.datepicker-dropdown .datepicker-years .datepicker-switch, .datepicker-dropdown .datepicker-months .datepicker-switch {
    pointer-events: none;
}

Solution 5:[5]

This worked for me using the Bootstrap Datepicker

  $('#datetimepicker1').datepicker({
        format: 'DD-MM-YYYY'            
    }).on('show', () => {
        const decadesDiv = document.querySelector(".datepicker-decades");
        if (decadesDiv) {
            decadesDiv.firstElementChild.firstElementChild.childNodes.forEach((tr,idx)  
            => {
                if (idx === 0)
                    tr.style.visibility = "hidden";
                else {
                    tr.children[1].addEventListener('click', (e) => { 
                      e.stopPropagation() });
                 //tr.children[1].style.visibility = "hidden"; //optional CSS 
                }
            });
        }
    });

Solution 6:[6]

Well for me MaxViewMode worked like a charm. By default, it is set as 4 but we can change it acc. to our preference

days(0) -> month(1) -> year(2) -> decade(3) -> century(4)

MaxViewMode: 2 - to limit users to go up to years view mode

my code to limit users to go only till year

$('#Datepicker').datepicker('destroy').datepicker({

        todayHighlight: true,
        endDate: endDate,
        maxViewMode: 2

Here is the documentation https://bootstrap-datepicker.readthedocs.io/en/latest/options.html#maxviewmode

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sarpe
Solution 2
Solution 3 Yatharth Varshney
Solution 4 Mayur Chauhan
Solution 5 chri3g91
Solution 6 Shadow