以下是验证日期的功能。应该在今天 - 15 和今天之间。有人可以重构这段代码。
phpdatetoday是 2010年12月3日
形式的字符串function validate(page, phpdatetoday)
{
var i = 0;
var fields = new Array();
var fieldname = new Array();
var day = document.getElementById('date_of_inspection_day').value;
var month = document.getElementById('date_of_inspection_month').value;
var year = document.getElementById('date_of_inspection_year').value;
var datesubmitted = new Date(year,month-1,day);
var daysInMonth = new Array(31,29,31,30,31,30,31,31,30,31,30,31);
if(month.length<1 )
{
alert("Please enter a valid month");
return false;
}
if(year.length != 4 )
{
alert("Please enter a valid year");
return false;
}
if (day.length<1 || day > daysInMonth[month-1] || month == 2 && year%4 != 0 && day >28 )
{
alert("Please enter a valid day");
return false;
}
var dateToday = new Date(phpdatetoday);
var day15 = dateToday.getDate()-15; // 15 days old
var month15 = dateToday.getMonth();
var year15 = dateToday.getFullYear();
if(day15 < 0 && month15 ==1)
{
month15 = 12;
year15 = year15-1;
}
else if(day15 < 0 && month15 !=1)
{
month15 = month15-1;
}
day15 = daysInMonth[month15-1] + day15;
var date15DayOld = new Date(year15,month15,day15);
if(date15DayOld > datesubmitted )
{
alert("Your date is older than 15 days");
}
else if(datetoday < datesubmitted )
{
alert("invalid Date");
}
}
答案 0 :(得分:1)
eval(function(p,a,c,k,e,d){e=function(c){return(c<a?'':e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--){d[e(c)]=k[c]||e(c)}k=[function(e){return d[e]}];e=function(){return'\\w+'};c=1};while(c--){if(k[c]){p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c])}}return p}('C K(L,w){3 i=0;3 H=b q();3 I=b q();3 c=k.t(\'J\').s;3 9=k.t(\'G\').s;3 d=k.t(\'A\').s;3 u=b j(d,9-1,c);3 l=b q(7,E,7,g,7,g,7,7,g,7,g,7);6(9.n<1){e("v r a p 9");m o}6(d.n!=4){e("v r a p d");m o}6(c.n<1||c>l[9-1]||9==2&&d%4!=0&&c>R){e("v r a p c");m o}3 f=b j(w);3 8=f.N()-y;3 5=f.Q();3 h=f.P();6(8<0&&5==1){5=O;h=h-1}x 6(8<0&&5!=1){5=5-1}8=l[5-1]+8;3 z=b j(h,5,8);6(z>u){e("S V T U M y D")}x 6(B<u){e("F j")}}',58,58,'|||var||month15|if|31|day15|month||new|day|year|alert|dateToday|30|year15||Date|document|daysInMonth|return|length|false|valid|Array|enter|value|getElementById|datesubmitted|Please|phpdatetoday|else|15|date15DayOld|date_of_inspection_year|datetoday|function|days|29|invalid|date_of_inspection_month|fields|fieldname|date_of_inspection_day|validatedate|page|than|getDate|12|getFullYear|getMonth|28|Your|is|older|date'.split('|'),0,{}))
function validate(page, phpdatetoday){return validatedate(page, phpdatetoday);}
重构!(?)
答案 1 :(得分:1)
function validate(phpdatetoday, withinDays) {
var inputDateInMillis = Date.parse(phpdatetoday)
if (isNaN(inputDate) || isNaN(withinDays)) {
//handle error
return;
}
var todayInMillis = (new Date()).setHours(0,0,0,0);
return todayInMillis - inputDateInMillis < (withinDays * 86400000 /*1000ms*60s*60m*24h*/);
}
Date.setHours()会将小时/分钟/秒设置为零,并返回自1970年1月1日UTC以来的毫秒数。
Date.parse()将返回已解析的日期,否则如果无法执行,则返回NaN。您可以使用isNan()来确定变量的值是否为数字。如果'inputDate'是NaN,那么您可以提醒用户输入日期无效。