如何获取两个日期之间选择的总天数?

泰罗克

我有这个jquery脚本,它允许用户选择开始日期和结束日期:

<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>

然后,我有一个文本框控件:

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

用户选择开始日期和结束日期后,我们希望在该框中输入总天数。

有什么想法如何计算吗?

克里斯托弗·雅伦(KristofferJälén)

onSelect选项添加到日期选择器并计算天数。

$('#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);
        }
    }
);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章