简体   繁体   English

查找从jQuery datepicker中选择的两个日期之间的差异

[英]Find difference between two dates picked from jQuery datepicker

I have two text boxes that pick dates via jQuery datepickers. 我有两个文本框,它们通过jQuery datepickers选择日期。 I want to access them in JavaScript and find the difference between them in terms of days. 我想用JavaScript访问它们,并找到它们之间的区别。

I am accessing the dates via clientID of the text boxes and simply taking the difference, but it is not working for me. 我正在通过文本框的clientID访问日期,只是采用了区别,但是它对我不起作用。 Is there some specific way of accessing date values from textboxes filled via datepicker and any special method of finding the difference in terms of days? 是否有某种特定的方法可以从通过datepicker填充的文本框中访问日期值,并且可以通过任何特殊的方法来找到天数之间的差异?

My date fields: 我的日期字段:

<td style="width: 15%">
    <asp:TextBox ID="txtStartDt" runat="server" Enabled="true" Width="80%" ValidationGroup="Save"></asp:TextBox>
    <img alt="Select Date" src="../Images/show-calendar.gif" border="0" style="width: 17px; height: 16px;"  onclick="javascript:calendarPicker('ContentPlaceHolder1_txtStartDt')" id="IMG1" />
</td>
<td style="width: 9%">
    <asp:Label ID="Label3" runat="server" CssClass="label">End Date:</asp:Label>
</td>
<td style="width: 248px">
    <asp:TextBox ID="txtEndDt" runat="server" Enabled="true" Width="126px"></asp:TextBox>
    <img alt="Select Date" src="../Images/show-calendar.gif" border="0" style="width: 17px; height: 16px;" onclick="javascript:calendarPicker('ContentPlaceHolder1_txtEndDt')" id="IMG2" />
</td>

My JavaScript: 我的JavaScript:

function CheckDuration() {
    var toDate1 = document.getElementById('<% =txtStartDt.ClientID%>');
    var toDate2 = new Date(toDate1.value.replace('-', ' '));

    var toDate = toDate2.setDate(toDate2.getDate());

    var toDate4 = document.getElementById('<% =txtEndDt.ClientID%>');
    var toDate5 = new Date(toDate1.value.replace('-', ' '));

    var toDate6 = toDate2.setDate(toDate2.getDate());

    if ((toDate6 - toDate) > 30)
        confirm("Selected time period is of more than 1 month duration");
}

There are built in methods to get a date from an input that has a datepicker, and that date will be a javascript date object, on which you can use functions like getTime() to get milliseconds from epoch, and then just subtract one from the other: 有内置的方法可以从具有日期选择器的输入中获取日期,该日期将是一个javascript日期对象,您可以在该对象上使用诸如getTime()类的函数从历元中获取毫秒,然后从中减去一个其他:

var from_date = $("#from_input").datepicker('getDate'),
    to_date   = $("#to_input").datepicker('getDate');

var diff_in_milliseconds = to_date.getTime() - from_date.getTime();

Now you'll have to figure out how many milliseconds there are in a day? 现在,您必须找出一天中有几毫秒?

EDIT: 编辑:

I'll add an example to this 我将为此添加一个示例

FIDDLE 小提琴

using the following Java script you would find the difference between date. 使用以下Java脚本,您会发现日期之间的差异。 Suppose there are two box with the name dtpEventDate = '06/12/2012' and dtpEndDate = '08/12/2012' 假设有两个框名为dtpEventDate = '06 / 12/2012'和dtpEndDate = '08 / 12/2012'

var fa = dtpEventDate.split('/')
var ta = dtpEndDate.split('/')

//  var a = new Date(fa[1]-1,fa[0],fa[2]);          
//  var d = new Date(ta[1]-1,ta[0],ta[2]); for MM-dd-yyyy

    var a = new Date(fa[2],fa[1]-1,fa[0]);
    var d = new Date(ta[2],ta[1]-1,ta[0]); // for dd-MM-yyyy

using var a and d we can find the difference between date 使用var a和d我们可以找到日期之间的差异

function CheckDuration() {
                //                alert("");

                var toDate1 = document.getElementById('<% =txtStartDt.ClientID%>');
                var toDate2 = new Date(toDate1.value.replace('-', ' '));

                var toDate = toDate2.setDate(toDate2.getDate());

                var toDate4 = document.getElementById('<% =txtEndDt.ClientID%>');
                var toDate5 = new Date(toDate4.value.replace('-', ' '));

                var toDate6 = toDate5.setDate(toDate5.getDate());

                if (toDate && toDate6) {
                       diff = ((toDate6 - toDate) / 86400000);

      //                        alert(diff);
                        if(diff>30)
                        return confirm("Selected time period is of more than 1 month duration");
                }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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