我有一个带有 id 的年份列表,从日期到现在如下:
- id from_date to_date
- 1 2013-02-12 2013-02-22
- 2 2013-03-01 2013-03-28
- 3 2013-03-29 2013-04-15 等
如果有条件,我会遇到重叠日期的问题。我想将 id 2 的 from_date 编辑为 2013-02-22 和 2013-03-01 之间的日期,或将期间 1 的 to_date 编辑为 2013-02-22 和 2013-03-01 之间的日期,但它只允许1 根据我下面的代码。
这是我的代码片段:
function validate(id){
//id is passed as a parameter to the function
from_date = document.getElementById('from_date_' + id);
to_date= document.getElementById('to_date_' + id);
var from = new Date();
var to = new Date();
// I am able to split the string, .split("-") and convert it in date format
previous_to_date = document.getElementById("to_" + id);//id is one less
next_from_date = document.getElementById("from_date_" + id);//id is one more
current_from_date //I store it before editing the from date
current_to_date //Similarly for current from date
//from_date and to_date are the edited dates by the user
if (from_date > to_date){ alert("This date is invalid"); return false; }
//now to check for overlapping dates
**//This is the condition I am having problems with**
if ((from_date < current_from_date && from_date < previous_to_date) || (to_date > current_to_date && to_date > next_from_date) && from_date > current_to_date){
alert("These dates overlap,Please select another date");
return false;
}
//successful so now we submit the form...
}