简体   繁体   中英

How do I grab total number of days selected between two dates?

I have this jquery script that allows a user to choose a start date and end date:

<script type="text/javascript" charset="utf-8">
    $(document).ready(function () {

        $('#txtToDate').datepicker({ showOn: 'button',
            buttonImage: 'images/20/calendar200.gif',
            buttonImageOnly: true, onSelect: function () { },
            onClose: function () { $(this).focus(); }
        });


        $('#txtFromDate').datepicker({ showOn: 'button',
            buttonImage: 'images/20/calendar200.gif',
            buttonImageOnly: true, onSelect:
        function (dateText, inst) {
            $('#txtToDate').datepicker("option", 'minDate', new Date(dateText));
        }
      ,
            onClose: function () { $(this).focus(); }
        });
        $('#txtToDate').datepicker({
       // Your other options here
        onSelect: function(dateText, inst) {
        var days = ($("#txtToDate").datepicker('getDate') - 
            $("#txtFromDate").datepicker('getDate'))/(24*60*60*1000);
        $('#numOfDays').val(days);
       console.log(days);
    }
  }
  );  

    });  
</script>

Then I have a textbox control:

<asp:TextBox ID="numOfDays" class="fptextbox12" runat="server" style="width:60px;"></asp:TextBox>

Once a user chooses a start date and an end date, we would like the total number of days in that box.

Any ideas how to calculate this?

Add the onSelect option to the datepicker and calculate the number of days.

$('#txtToDate').datepicker({
        // Your other options here
        onSelect: function(dateText, inst) {
            var days = ($("#txtToDate").datepicker('getDate') - 
                $("#txtFromDate").datepicker('getDate'))/(24*60*60*1000);
            $('#numOfDays').val(days);
        }
    }
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM