我正在使用日期选择器来选择日期,我想计算选择日期和日期之间的差异。然后警告差异。我看不到代码工作
HTML datepicker
<input type="date" size="8" name="advDurFrom" />
<input type="date" size="8" name="advDurTo"/>
的Javascript
$('input[name=advDurFrom]').click(function() {
var x=$('input[name=advDurFrom]').val();
var date1 = new Date(x);
});
$('input[name=advDurTo]').click(function() {
var y=$('input[name=advDurTo]').val();
var date2 = new Date(y);
});
$('input[name=advDurTo]').focusout(function() {
var diffDays = date2.getTime() - date1.getTime();
alert(diffDays);
});
答案 0 :(得分:1)
HTML:
<input type="date" name="startdate">
<input type="date" name="enddate">
<button id="calculate">Calculate</button>
<h2 id="result"></h2>
脚本:
var startdateInput = document.querySelector('input[name="startdate"]'),
enddateInput = document.querySelector('input[name="enddate"]'),
calculateButton = document.getElementById('calculate'),
resultElement = document.getElementById('result');
calculateButton.addEventListener('click', function(e) {
if( startdateInput.value && enddateInput.value ) {
result.textContent = new Date(enddateInput.value) - new Date(startdateInput.value);
}
});
或者看看JSFiddle http://jsfiddle.net/zJTfM/
结果是开始日期和结束日期之间的毫秒数。
答案 1 :(得分:0)
$('input[name=advDurFrom]').live('change', function(e) {
var x=$(this).datepicker( "getDate" );
date1 = x.getTime();
});
$('input[name=advDurTo]').live('change', function(e) {
var y=$(this).datepicker( "getDate" );
date2 = y.getTime();
});
$('input[name=advDurTo]').live('blur', function(e) {
if(date2 && date1){
var diffDays = date2 - date1 ;
alert(diffDays);
} else {
alert("date is not selected.")
}
});
答案 2 :(得分:0)
在我的项目中,我使用此函数来计算2日期之间的差异日,我会为您修改一些。 see demo in jsfiddle
HTML:
<input type="text" size="8" name="advDurFrom" />
<input type="text" size="8" name="advDurTo"/>
difference days: <span class="diff"><span>
JS:
function CalendarDays(startDate, endDate) {
if (endDate < startDate)
return 0;
// Calculate days between dates
var millisecondsPerDay = 86400 * 1000; // Day in milliseconds
startDate.setHours(0, 0, 0, 1); // Start just after midnight
endDate.setHours(23, 59, 59, 999); // End just before midnight
var diff = endDate - startDate; // Milliseconds between datetime objects
var days = Math.round(diff / millisecondsPerDay);
return days;
}
function common_getDateFromUI(str) {
var arr = str.split("/");
var returnDate = new Date(arr[2], arr[1] - 1, arr[0], 0, 0, 0, 0);
return returnDate;
}
$().ready(function(){
$('input[name="advDurFrom"],input[name="advDurTo"]').datepicker( {
showOn : "both",
dateFormat : "dd/mm/yy",
changeMonth : true,
changeYear : true,
buttonImageOnly : true,
onSelect : function(dateText, inst) {
var day1 = common_getDateFromUI($('input[name="advDurFrom"]').val());
var day2 = common_getDateFromUI($('input[name="advDurTo"]').val());
$(".diff").html(CalendarDays(day1,day2));
}
});
});
答案 3 :(得分:0)
我认为你需要全局声明变量。试试这个代码。这对我很有用。
var date1 = "";
var date2 = "";
$('#advDurFrom').click(function () {
var x = $(this).val();
date1 = new Date(x);
});
$('#advDurTo').click(function () {
var y = $(this).val();
date2 = new Date(y);
});
$('#advDurTo').focusout(function () {
var diffDays = date2.getTime() - date1.getTime();
alert(diffDays);
});
并将HTML更改为
<input type="date" size="8" id="advDurFrom" />
<input type="date" size="8" id="advDurTo" />
答案 4 :(得分:0)
getTime返回毫秒,你只需要转换成正确的单位。这里有一个天差异的例子。也可以使用模糊事件而不是单击,或者在用户实际输入之前最终分配值
var isOK = false;
var isOK2 = false;
$('input[name=advDurFrom]').blur(function () {
var x = $('input[name=advDurFrom]').val();
try {
date1 = new Date(x);
isOK = !isNaN(date1);
} catch (e) {
isOK = false;
}
printDiff();
});
$('input[name=advDurTo]').blur(function () {
var y = $('input[name=advDurTo]').val();
try {
date2 = new Date(y);
isOK2 = !isNaN(date2);
} catch (e) {
isOK2 = false;
}
printDiff();
});
function printDiff(){
if (isOK && isOK2) {
var one_day = 1000 * 60 * 60 * 24;
var diff = Math.ceil((date2.getTime() - date1.getTime()) / (one_day));
console.log(diff + ' days');
}
}