找到从jQuery datepicker选择的两个日期之间的差异 [英] Find difference between two dates picked from jQuery datepicker

查看:730
本文介绍了找到从jQuery datepicker选择的两个日期之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个文本框,通过jQuery日期检查器选择日期。
我想使用JavaScript访问它们,并在日期方面找到它们之间的区别。



我正在通过文本框的clientID访问日期取而代之,但对我来说并不奏效。
有没有一些特定的方式来访问通过日期戳填充的文本框中的日期值,以及任何寻找天数差异的特殊方法?



我的日期字段:

 < td style =width:15%> 
< asp:TextBox ID =txtStartDtrunat =serverEnabled =trueWidth =80%ValidationGroup =Save>< / asp:TextBox>
< img alt =选择日期src =../ Images / show-calendar.gifborder =0style =width:17px; height:16px; onclick =javascript:calendarPicker('ContentPlaceHolder1_txtStartDt')id =IMG1/>
< / td>
< td style =width:9%>
< asp:Label ID =Label3runat =serverCssClass =label>结束日期:< / asp:Label>
< / td>
< td style =width:248px>
< asp:TextBox ID =txtEndDtrunat =serverEnabled =trueWidth =126px>< / asp:TextBox>
< img alt =选择日期src =../ Images / show-calendar.gifborder =0style =width:17px; height:16px; onclick =javascript:calendarPicker('ContentPlaceHolder1_txtEndDt')id =IMG2/>
< / td>

我的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());

如果((toDate6 - toDate)> 30)
confirm(选定的时间段超过1个月的持续时间);
}


解决方案

内置方法从具有datepicker的输入获取日期,该日期将是一个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();

现在你必须弄清楚一天有多少毫秒?



编辑:



我将为此添加一个例子

FIDDLE


I have two text boxes that pick dates via jQuery datepickers. I want to access them in JavaScript and find the difference between them in terms of days.

I am accessing the dates via clientID of the text boxes and simply taking the difference, but it is not working for me. 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?

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:

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:

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

这篇关于找到从jQuery datepicker选择的两个日期之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆