![](/img/trans.png)
[英]jquery datepicker change value of input, based on difference between two dates
[英]Find difference between two dates picked from jQuery datepicker
我有两个文本框,它们通过jQuery datepickers选择日期。 我想用JavaScript访问它们,并找到它们之间的区别。
我正在通过文本框的clientID访问日期,只是采用了区别,但是它对我不起作用。 是否有某种特定的方法可以从通过datepicker填充的文本框中访问日期值,并且可以通过任何特殊的方法来找到天数之间的差异?
我的日期字段:
<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>
我的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");
}
有内置的方法可以从具有日期选择器的输入中获取日期,该日期将是一个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();
现在,您必须找出一天中有几毫秒?
编辑:
我将为此添加一个示例
使用以下Java脚本,您会发现日期之间的差异。 假设有两个框名为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
使用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.