![](/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.