0 in from 1-9 do not work in the calendar ie all dates are highlighted but that's all from 1 to 9 is not as I understand it, you need a zero but how to add it to the code that would work

("0" + curr_date).slice(-2) 

I know there is such a function, but it does not work

  var active_dates = ["11/12/2016", "13/12/2016", "01/01/2017", "15/01/2017", "16/01/2017"]; $("#datepicker").datepicker({ format: "dd/mm/yyyy", todayHighlight: true, maxViewMode: 0, beforeShowMonth: 1, beforeShowDay: function(date){ var d = date; var curr_date = d.getDate(); var curr_month = d.getMonth() + 1; //Months are zero based var curr_year = d.getFullYear() ; if(curr_month<10)curr_month = "0"+curr_month; if(curr_date<10)curr_date = "0"+curr_month; var formattedDate = curr_date + "/" + curr_month + "/" + curr_year if ($.inArray(formattedDate, active_dates) != -1){ return { classes: 'booked ' }; } return; } }); 
 .booked{ background: #F00!important; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link href="http://netdna.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap.min.css" rel="stylesheet"/> <link href="http://bsdp-assets.blackcherry.us/1.3.0/datepicker3.min.css" rel="stylesheet"/> <script src="http://bsdp-assets.blackcherry.us/1.3.0/bootstrap-datepicker.min.js"></script> <div id="datepicker"></div> <input type="hidden" id="my_hidden_input"> 

    3 answers 3

     function addLeadingZero (n) { return (n < 10) ? '0' + n : n; } 

       const format = ( value, length ) => `${"0".repeat(length - value.toString().length)}${value}`; console.log(format(9, 2)); console.log(format(12, 2)); console.log(format(10, 3)); 

      The same, but with the help of recursion -

       const format = ( value, length ) => value.toString().length < length ? format( "0" + value.toString(), length ) : value; console.log(format(9, 2)); console.log(format(12, 2)); console.log(format(10, 3)); 

         var active_dates = ["15/11/2016", "01/01/2017"]; $("#datepicker").datepicker({ format: "dd/mm/yyyy", todayHighlight: true, maxViewMode: 0, beforeShowMonth: 1, beforeShowDay: function(date){ var d = date; var day = d.getDate() ; var curr_date = "0" + day; var month = d.getMonth() + 1; //Months are zero based var curr_month = "0" + month; var curr_year = d.getFullYear() ; if (curr_date == 00) { curr_date = 10; } if (curr_date.length > 2){ curr_date = (curr_date).slice(1); } if (curr_month == 00) { curr_month = 12; } if (curr_month.length > 2){ curr_month = (curr_month).slice(1); } var formattedDate = curr_date + "/" + curr_month + "/" + curr_year if ($.inArray(formattedDate, active_dates) != -1){ return { classes: 'booked ' }; } return; } }); 
         .booked{ background: #F00!important; } 
         <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link href="http://netdna.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap.min.css" rel="stylesheet"/> <link href="http://bsdp-assets.blackcherry.us/1.3.0/datepicker3.min.css" rel="stylesheet"/> <script src="http://bsdp-assets.blackcherry.us/1.3.0/bootstrap-datepicker.min.js"></script> <div id="datepicker"></div> <input type="hidden" id="my_hidden_input">