js表示验证日期范围

时间:2010-06-23 03:30:01

标签: javascript

任何人都可以帮助我....如何验证此问题的javascript日期:

用户选择开始日期和结束日期将从开始日期开始选择超过90天。如果结束日期低于90天,用户可以发送表格...

from date code :
<input name="fromDate" type="text" id="fromDate" size="10" maxlength="10" onChange="">
<a href="#" name="anchorFrmDate" id="anchorFrmDate" onClick="calFrmDate.select(document.forms[0].fromDate,anchorFrmDate,'dd/MM/yyyy'); return false;">DATE</a>

to date code:
<input name="Todate" type="text" id="Todate" size="10" maxlength="10" onChange="">
a href="#" onClick="calToDte.select(document.forms[0].Todate,'anchorToDte','dd/MM/yyyy'); return false;" name="'anchorToDte'" id="'anchorToDte'"><strong>DATE</strong></a>

我已经尝试过,我的代码在下面......但是没有成功......我犯了哪个错误

function checkDateRange(fromDate, Todate) { 
   // Parse the entries 
   var startDate = Date.parse(fromDate); 
   var endDate = Date.parse(Todate); 
   // Make sure they are valid 
    if (isNaN(startDate)) { 
      alert("The start date provided is not valid, please enter a valid date."); 
      return false; 
   } 
   if (isNaN(endDate)) { 
       alert("The end date provided is not valid, please enter a valid date."); 
       return false; 
   } 
   // Check the date range, 86400000 is the number of milliseconds in one day 
   var difference = (endDate - startDate) / (7776000000); 
   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; 
}

1 个答案:

答案 0 :(得分:0)

这是一个草图:

var startDateText = ...
var NINETY_DAYS = 1000 * 3600 * 24 * 90;
var startDate = new Date(startDateText);
var maxEndDate = startDate.getTime() + NINETY_DAYS;
..
var endDateText = ...
var endDate = new Date(endDateText);
if (endDate.getTime() > maxEndDate) {
    Window.alert("Too late");
}