Search code examples
javascriptjquerytwitter-bootstrapbootstrap-datepicker

Date range not selected in calendar after value changed programmatically


I am using BootStrap Datepicker daterange feature. When I set the dates programmatically, the calendars don't display correctly the selection.
My codes:

<div class="input-group input-daterange">
    <input type="text" id="startDate"
        class="form-control" name="startDate" value="2020-08-01"> 
    <span class="input-group-addon">to</span> 
    <input type="text" id="endDate" 
        class="form-control" name="endDate" value="2020-08-08">
</div> 

<script>
$(document).ready(function() {
    $('.input-daterange').datepicker( { 
        format: 'yyyy-mm-dd',
        autoclose: true
    });         
       
    //change date
    $('#startDate').val('2022-01-19');  
    $('#endDate').val('2022-01-29');      
    
    var startDate = new Date($('#startDate').val());
    var endDate = new Date($('#endDate').val());

    var $start = $(".input-daterange").find('#startDate');
    var $end = $(".input-daterange").find('#endDate')   
    $start.datepicker('setStartDate', startDate);
    $end.datepicker('setEndDate', endDate);
});     
</script>

Expected behaviour enter image description here

Actual behaviour enter image description here

Other than the calendar display, there are 2 more problems:

  1. Start date calendar doesn't auto close after selected

  2. Start date doesn't use the format (yyyy-mm-dd). After selected it become mm/dd/yyyy

Version used

datepicker : v1.9.0
bootstrap: v3.4.1
jquery: v3.4.1

Update on 22-Jun:
I changed my code to:

$start.datepicker('setDate', startDate);
$end.datepicker('setDate', endDate); 

Now it is better:

enter image description here

Nevertheless, this is not what I expected. I don't mind 29th is not highlighted with darker grey in start date calendar. The real problem is 19th is not highlighted/ selected at all in end date calendar.


Solution

  • Code/Demo:

    <!doctype html>
    <html lang="en">
      <head>
        <title>Title</title>
        <!-- Required meta tags -->
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.9.0/css/bootstrap-datepicker.min.css">
        <!-- Bootstrap CSS -->
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
      </head>
      <body>
        <div class="container">
            <div class="input-group input-daterange">
                <input type="text" id="startDate"
                    class="form-control" name="startDate" value="2020-08-01"> 
                <span class="input-group-addon">to</span> 
                <input type="text" id="endDate" 
                    class="form-control" name="endDate" value="2020-08-08">
            </div> 
        </div>
        <!-- Optional JavaScript -->
        
        <!-- jQuery first, then Popper.js, then Bootstrap JS -->
        <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.9.0/js/bootstrap-datepicker.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
        <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
        <script>
            $(document).ready(function() {
                $('.input-daterange').datepicker( {
                    format: 'yyyy-mm-dd',
                    autoclose: true,
                });         
                   
                //change date
                $('#startDate').val('2022/01/19');  
                $('#endDate').val('2022/01/29');      
                
                var startDate = new Date($('#startDate').val());
                var endDate = new Date($('#endDate').val());
            
                var $start = $(".input-daterange").find('#startDate');
                var $end = $(".input-daterange").find('#endDate')   
                $start.datepicker('setDate', startDate);
                $end.datepicker('setDate', endDate);
            });     
            </script>
        </body>
    </html>

    Explanation:

    I changed this from:

    $start.datepicker('setStartDate', startDate);
    $end.datepicker('setEndDate', endDate);
    

    to:

    $start.datepicker('setDate', startDate);
    $end.datepicker('setDate', endDate);
    

    I also had to change:

     $('#startDate').val('2022-01-19');  
     $('#endDate').val('2022-01-29');
    

    to:

    /*UPDATED from using dashes to slashes*/
     $('#startDate').val('2022/01/19');  
     $('#endDate').val('2022/01/29');
    

    Additionally, from your code by default it seemed to keep the right format that you wanted and the startdate datepicker closed for me upon clicking or entering a new date as you can see in the snippet above. Let me know if you have any questions.