我正在尝试比较两个datepicker
日期,看看它们是否相隔超过7天。
我该怎么做?
我通常会看到他们的差异是否大于7,但这不会占数月等等。
这是我的代码:
var datepickerBegin = $("#datepicker_start").val();
var datepickerEnd = $("#datepicker_to").val();
if (datepickerBegin - datepickerEnd > 7) {
alert('more than a week apart!')
}
任何提示?
答案 0 :(得分:18)
使用$("#datepicker_xxx").datepicker("getDate")
将选择的日期设为Date
。
end - begin > 7 * 86400 * 1000
答案 1 :(得分:2)
//检查日期范围,86400000是一天内的毫秒数
var difference = (datepickerEnd- datepickerBegin ) / (86400000 * 7);
if (difference < 0) {
alert("The start date must come before the end date.");
return false;
}
if (difference <= 1) {
alert("The range must be at least seven days apart.");
return false;
}
return true;
答案 2 :(得分:2)
试试这个,DatePicker有一个方便的formatDate函数,我用它来比较mm / dd / yy日期:
$.datepicker.formatDate("dd/mm/yy",new Date("09/01/2014")) < $.datepicker.formatDate("dd/mm/yy", new Date("10/01/2014")); // Returns true
$.datepicker.formatDate("dd/mm/yy",new Date("10/01/2014")) < $.datepicker.formatDate("dd/mm/yy", new Date("10/01/2014")); // Returns false
$.datepicker.formatDate("dd/mm/yy",new Date("11/01/2014")) < $.datepicker.formatDate("dd/mm/yy", new Date("10/01/2014")); // Returns false
&#13;
答案 3 :(得分:1)
试试这个:
var datepickerBegin = $("#datepicker_start").val(); // lets, returning in mm/dd/yy format
var datepickerEnd = $("#datepicker_to").val(); // lets, returning in mm/dd/yy format
if ( ($.datepicker.parseDate('mm/dd/yy', datepickerBegin) - $.datepicker.parseDate('mm/dd/yy', datepickerEnd)) > 7) {
alert('more than a week apart!') ;
}
希望它应该有效。谢谢。更多check it出来。
答案 4 :(得分:1)
$(document).ready(function(){
$("#txtFromDate").datepicker({
minDate: 0,
maxDate: "+60D",
numberOfMonths: 2,
onSelect: function(selected) {
$("#txtToDate").datepicker("option","minDate", selected)
}
});
$("#txtToDate").datepicker({
minDate: 0,
maxDate:"+60D",
numberOfMonths: 2,
onSelect: function(selected) {
$("#txtFromDate").datepicker("option","maxDate", selected)
}
});
});
来自:<input type="text" id="txtFromDate" />
收件人:<input type="text" id="txtToDate" />
答案 5 :(得分:0)
这完美地运作
function checkDateDifference(startDate, endDate) {
startDate = $.datepicker.parseDate('mm/dd/yy', startDate);
endDate = $.datepicker.parseDate('mm/dd/yy', endDate);
var difference = (endDate - startDate) / (86400000);
alert(difference)
if (difference < 0) {
showError("The start date must come before the end date.");
return false;
}
return true;
}