我想比較兩個日期字段..我已經得到了相當接近,它甚至能正常工作..比較日期字段
但是,當「TODATE」字段是
leap year>= "FromDate"
問題就來了
對於如:
from date= 29/02/1996
to date= 29/02/1996
to date= 29/02/1992
to date= 29/02/1995
代碼工作很好..
但如果
from date= 29/03/1996
to date= 29/02/1996
代碼失敗......
這是我的代碼
function isDate(value)
{
var fromDate = document.getElementById("fromDate").value
var toDate= document.getElementById("toDate").value
var dateRegEx = null;
dateRegEx = new RegExp(/^(((0[1-9]|[12]\d|3[01])\/(0[13578]|1[02])\/((19|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)\/(0[13456789]|1[012])\/((19|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])\/02\/((19|[2-9]\d)\d{2}))|(29\/02\/((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$/g);
if (dateRegEx.test(fromDate)){
}
else{
alert("Invalid from date");
return false;
}
dateRegEx = new RegExp(/^(((0[1-9]|[12]\d|3[01])\/(0[13578]|1[02])\/((19|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)\/(0[13456789]|1[012])\/((19|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])\/02\/((19|[2-9]\d)\d{2}))|(29\/02\/((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$/g);
if(dateRegEx.test(toDate)) {
}
else{
alert("Invalid to date");
return false;
}
var stDate = new Date(fromDate);
var enDate = new Date(toDate);
var compDate = enDate - stDate;
if(compDate > 0)
return true;
else
{
alert("Please Enter the correct date ");
return false;
}
/**/
}
如何使所有閏年工作...在那裏我將不得不作出的代碼更改。
Possibile重複:http://stackoverflow.com/questions/327429/whats-the-best-way-to-calculate-date-difference-in-javascript – 2013-05-10 12:11:55
添加一個額外的條件來檢查閏年 – 999k 2013-05-10 12:15:26
兩個日期領域單獨工作很好... – razor 2013-05-10 12:18:16