检查日期这些日期失败我需要更改什么?
20 Jan 2013
20 jan 2013
20 JAN 2013
2 Jan 2013
30 Jan 2013
20 feb 2013
20 jun 2013
代码:
function ValidateFutureDate(alertstring,pCheckDate)
{
// Default Return Variable to return success.
var returnSuccess = 1;
// setup a temporary array.
var temp = new Array();
// split the date into the array.
temp = pCheckDate.split(' ');
// Setup the Test Date
var testDate = new Date ( temp[1]+" "+temp[0]+", "+temp[2] );
var testMonth=testDate.getMonth()+1;
if (testMonth<10)
{
testMonth = "0"+testDate.getMonth();
}
// This is the Date to test if it is a valid date.
var testIsDate = ( testMonth+"/"+temp[0]+"/"+temp[2] );
// Is this a valid Date
if(!(isDate(testIsDate))) {
returnSuccess = 0;
} else if
// Check to see if this date is greater then today.
(!(testDate.getTime() > (new Date().getTime()))) {
//return Failure zero is a failure.
returnSuccess = 0;
alertstring = alertstring + 'Expiration must be in the future. \n';
}
// Return the Success or Failure of the Date Format Validation
return returnSuccess;
}
答案 0 :(得分:1)
您的代码的以下部分不太正确:
var testDate = new Date ( temp[1]+" "+temp[0]+", "+temp[2] );
var testMonth=testDate.getMonth()+1;
if (testMonth<10)
{
testMonth = "0"+testDate.getMonth(); // change this line
}
当您初始化testMonth
时,您记得添加1,因为.getMonth()
会返回从零开始的月份,但是在if测试中,您再次使用.getMonth()
而不添加1.您应该更改那到testMonth = "0" + testMonth;
。
(另外,不是问题,但是当您创建temp
时,在下一行将其分配给新的数组时,没有必要将其初始化为新数组。)
抱歉,我必须这样做,我没有时间在您的代码中查找任何其他问题。
答案 1 :(得分:0)
isDate
看起来如何?
我检查了以下内容(testMonth = "0" + testMonth
更改):
document.write( ""+ValidateFutureDate("---","20 Jan 2013"));
document.write( ""+ValidateFutureDate("---","20 jan 2013"));
document.write( ""+ValidateFutureDate("---","20 JAN 2013"));
document.write( ""+ValidateFutureDate("---","2 Jan 2013"));
document.write( ""+ValidateFutureDate("---","30 Jan 2013"));
document.write( ""+ValidateFutureDate("---","20 feb 2013"));
document.write( ""+ValidateFutureDate("---","20 jun 2013"));
与
function isDate(date){
return true;
}
并打印1次7次(Firefox / Opera)。