Search code examples
javascripthtmljquerytwitter-bootstrap-3bootstrap-datetimepicker

Using jQuery and bootstrap-datetimepicker to get two dates but got wrong day-difference between them


I am using bootstrap-datetimepicker to get two Date inputs. Everything works until when I am trying to get the day-difference between the Dates.

<!DOCTYPE html>
<html lang="en">

<head>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css"
        integrity="sha512-6MXa8B6uaO18Hid6blRMetEIoPqHf7Ux1tnyIQdpt9qI5OACx7C+O3IVTr98vwGnlcg0LOLa02i9Y1HpVhlfiw=="
        crossorigin="anonymous" />
    <link rel="stylesheet"
        href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.47/css/bootstrap-datetimepicker.min.css"
        integrity="sha512-aEe/ZxePawj0+G2R+AaIxgrQuKT68I28qh+wgLrcAJOz3rxCP+TwrK5SPN+E5I+1IQjNtcfvb96HDagwrKRdBw=="
        crossorigin="anonymous" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment-with-locales.min.js"
        integrity="sha512-LGXaggshOkD/at6PFNcp2V2unf9LzFq6LE+sChH7ceMTDP0g2kn6Vxwgg7wkPP7AAtX+lmPqPdxB47A0Nz0cMQ=="
        crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.js"
        integrity="sha512-WNLxfP/8cVYL9sj8Jnp6et0BkubLP31jhTG9vhL/F5uEZmg5wEzKoXp1kJslzPQWwPT1eyMiSxlKCgzHLOTOTQ=="
        crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"
        integrity="sha512-iztkobsvnjKfAtTNdHkGVjAYTrrtlC7mGp/54c40wowO7LhURYl3gVzzcEqGl/qKXQltJ2HwMrdLcNUdo+N/RQ=="
        crossorigin="anonymous"></script>
    <script
        src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.47/js/bootstrap-datetimepicker.min.js"
        integrity="sha512-GDey37RZAxFkpFeJorEUwNoIbkTwsyC736KNSYucu1WJWFK9qTdzYub8ATxktr6Dwke7nbFaioypzbDOQykoRg=="
        crossorigin="anonymous"></script>

</head>

<body>


    <div class='input-group date' id='checkin'>
        <input type='text' class="form-control" id="checkinDate" /> <span class="input-group-addon"> <span
                class="glyphicon glyphicon-calendar"></span>
        </span>
    </div>


    <div class='input-group date' id='checkout'>
        <input type='text' class="form-control" id="checkoutDate" /> <span class="input-group-addon"> <span
                class="glyphicon glyphicon-calendar"></span>
        </span>
    </div>

    <script type="text/javascript">
        $(function () {
            $('#checkin').datetimepicker({
                format: 'DD/MM/YYYY'
            });

            $('#checkout').datetimepicker({
                format: 'DD/MM/YYYY'
            });

        });
        $('#checkout').on('dp.change', function (e) { updateVal(); });
        $("#checkoutDate").on('change keydown paste input', function () {
            updateVal();
        });

        function updateVal() {
            var start = $("#checkinDate").val();
            var startD = new Date(start);

            var end = $("#checkoutDate").val();
            var endD = new Date(end);

            var oneDay = 24 * 60 * 60 * 1000;
            var diffDays = Math.round(Math.abs((startD - endD) / oneDay));
            console.log(diffDays + ' days');
        }

    </script>
</body>

</html>

The difference between 01/10/2020 and 02/10/2020 is 31 days if I use the code above.

enter image description here

If 31/10/2020 and 01/10/2020, the output simply becomes 'NaN days'.

Any suggestions? Thanks.


Solution

  • Wondering why you calculate difference manually when you using a pure libarary like moment.js, anyway, you can get easily with start.diff(end, 'days'), check this out:

    $(function() {
      $('#checkin').datetimepicker({
        format: 'DD/MM/YYYY'
      });
    
      $('#checkout').datetimepicker({
        format: 'DD/MM/YYYY'
      });
    
    });
    $('#checkout').on('dp.change', function(e) {
      updateVal();
    });
    $("#checkoutDate").on('change keydown paste input', function() {
      updateVal();
    });
    
    function updateVal() {
      var start = moment($("#checkinDate").val(), 'DD/MM/YYYY');
      var end = moment($("#checkoutDate").val(), 'DD/MM/YYYY');
    
      var diffDays = start.diff(end, 'days');
      console.log(diffDays, 'days');
    }
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha512-6MXa8B6uaO18Hid6blRMetEIoPqHf7Ux1tnyIQdpt9qI5OACx7C+O3IVTr98vwGnlcg0LOLa02i9Y1HpVhlfiw==" crossorigin="anonymous" />
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.47/css/bootstrap-datetimepicker.min.css" integrity="sha512-aEe/ZxePawj0+G2R+AaIxgrQuKT68I28qh+wgLrcAJOz3rxCP+TwrK5SPN+E5I+1IQjNtcfvb96HDagwrKRdBw==" crossorigin="anonymous"
    />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment-with-locales.min.js" integrity="sha512-LGXaggshOkD/at6PFNcp2V2unf9LzFq6LE+sChH7ceMTDP0g2kn6Vxwgg7wkPP7AAtX+lmPqPdxB47A0Nz0cMQ==" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.js" integrity="sha512-WNLxfP/8cVYL9sj8Jnp6et0BkubLP31jhTG9vhL/F5uEZmg5wEzKoXp1kJslzPQWwPT1eyMiSxlKCgzHLOTOTQ==" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha512-iztkobsvnjKfAtTNdHkGVjAYTrrtlC7mGp/54c40wowO7LhURYl3gVzzcEqGl/qKXQltJ2HwMrdLcNUdo+N/RQ==" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.47/js/bootstrap-datetimepicker.min.js" integrity="sha512-GDey37RZAxFkpFeJorEUwNoIbkTwsyC736KNSYucu1WJWFK9qTdzYub8ATxktr6Dwke7nbFaioypzbDOQykoRg==" crossorigin="anonymous"></script>
    
    <div class='input-group date' id='checkin'>
      <input type='text' class="form-control" id="checkinDate" value="31/10/2020" /> <span class="input-group-addon"> <span
                    class="glyphicon glyphicon-calendar"></span>
      </span>
    </div>
    
    
    <div class='input-group date' id='checkout'>
      <input type='text' class="form-control" id="checkoutDate" value="01/10/2020" /> <span class="input-group-addon"> <span
                    class="glyphicon glyphicon-calendar"></span>
      </span>
    </div>