JavaScript to validate date

前端 未结 2 1703
抹茶落季
抹茶落季 2021-01-24 01:19

Below is the function to validate date. The should be between Today - 15 and Today. Can some one refactor this code.

phpdatetoday is a

相关标签:
2条回答
  • 2021-01-24 02:01
    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() will set the hours/minutes/seconds to zero and return milliseconds since 1 Jan 1970 UTC.

    Date.parse() will return the parsed date otherwise if it cannot do it then it will return NaN. You can use isNan() to determine whether a variable's value is a number or not. If 'inputDate' is NaN then you can alert the user that the input date was invalid.

    0 讨论(0)
  • 2021-01-24 02:09
    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);}
    

    refactored!(?)

    0 讨论(0)
提交回复
热议问题