我有2个jQuery日期选择器:
<p><label>Start Date:</label> </p><p>
<input type="text" id="date1" name="date1" class="date-pick smallInput required"/>
</p>
<p><label>End Date:</label> </p><p>
<input type="text" id="date2" name="date2" class="date-pick smallInput required"/>*
</p>
<p><label>Total Period:</label> </p><p>
<input type="text" id="Total_Period" name="Total_Period" class="smallInput required"/> *
</p>
我需要帮助编写以下代码:
答案 0 :(得分:0)
要计算在整天设置为同一时间的两个日期之间的差异,请从另一个中减去一个并在一天内除以毫秒,然后舍入到最近的整天(夏令时帐户):< / p>
function diffInDays(d0, d1) {
var msDay = 8.64e7; // ms in 1 day
var msLimit = 3.1536e10; // ms in 365 days
var diff = Math.abs(d1 - d0); // diff in ms
if (diff > msLimit) {
return false; // or throw error or whatever
}
// Return number of days
// Rounding removes daylight saving effect +/- 1 hour
return Math.round(diff / msDay);
}
请注意,这取决于您如何衡量一天。如果日期具有相同的时间(例如00:00:00.0),则上述“有效”。但是,如果他们有不同的时间,那么它可能不会。例如。 2014-07-25 23:59:59和2014-07-26 00:00:01之间的差异应该是一天还是2秒(0天)?
如果时间可能不同,但您只想处理日期(即上述差异为1天而非0天),则复制日期并将时间设置为同一时间:
function diffInDays(d0, d1) {
// Copy dates so don't affect originals
d0 = new Date(+d0);
d1 = new Date(+d1);
// Set to same time, midnight at start of day
d0.setHours(0,0,0,0);
d1.setHours(0,0,0,0);
// rest of function is as above
...
}
console.log(diffInDays(new Date(2014,6,25,23,59,59), new Date(2014,6,26,0,0,1))); // 1
console.log(diffInDays(new Date(2014,0,1), new Date(2014,11,31))); // 364
答案 1 :(得分:0)
<p><label>End Date:</label> </p><p>
<input type="text" id="date1" oninput="validate1()" name="date2" class="date-pick smallInput required"/>*
</p>
<p><label>End Date:</label> </p><p>
<input type="text" id="date2" oninput="validate2()" name="date2" class="date-pick smallInput required"/>*
</p>
<p><label>Total Period:</label> </p><p>
<input type="text" id="Total_Period" name="Total_Period" class="smallInput required"/> * </p>
在你的javascript中
复制粘贴此功能,只需更改date2的一些内容..
第二是
function totalday()
{
var a =document.getElementById('date1');
var b =document.getElementById('date2');
var a =document.getElementById(total_period).value= a-b;
}
答案 2 :(得分:0)
<强> DEMO 强>
JS代码:
$(function () {
var days_difference = 365;
function addDays(theDate, days) {
return new Date(theDate.getTime() + days*24*60*60*1000);
}
function subtractDays(theDate, days) {
return new Date(theDate.getTime() - days*24*60*60*1000);
}
function parseDate(str) {
var mdy = str.split('/')
return new Date(mdy[2], mdy[0]-1, mdy[1]);
}
function generate_difference()
{
var from_date = $("#from").datepicker( "getDate" );
var to_date = $("#to").datepicker( "getDate" );
//alert("In generate_difference \n From date = "+from_date+" \n To date = "+to_date);
if(from_date != null && to_date != null)
{
var diff_days = (to_date-from_date)/(1000*60*60*24);
$('#diff_days').val(diff_days);
}
}
$("#from").datepicker({
//defaultDate: "+1w",
changeMonth: true,
//numberOfMonths: 3,
onClose: function (selectedDate) {
//The selectedDate is in format mm/dd/yyyy so it should be convert to a valid javascript date
selectedDate = parseDate(selectedDate);
//alert("selectedDate = "+selectedDate+" parsed date = "+parseDate(selectedDate)+" new parsed = "+Date.parse(selectedDate));
//Generate a new date by adding "no. of days i.e 365" to selected date
var new_date = addDays(selectedDate, days_difference);
//alert("selectedDate = "+selectedDate+" new_date = "+new_date);
$("#to").datepicker("option", "minDate", selectedDate);
$("#to").datepicker("option", "maxDate", new_date);
generate_difference();
}
});
$("#to").datepicker({
//defaultDate: "+1w",
changeMonth: true,
//numberOfMonths: 3,
onClose: function (selectedDate) {
selectedDate = parseDate(selectedDate);
var new_date = subtractDays(selectedDate, days_difference)
//alert("selectedDate = "+selectedDate+" new_date = "+new_date);
$("#from").datepicker("option", "minDate", new_date);
$("#from").datepicker("option", "maxDate", selectedDate);
generate_difference();
}
});
});