Link to home
Start Free TrialLog in
Avatar of clintnash
clintnashFlag for United States of America

asked on

convert 2 digit year to 4 digit

Hello.

    In the interest of making my application friendly to whiny users, I need to allow a date to be entered as mm/dd/yy AND mm/dd/yyyy.  I have modified my mask to allow this and everything works except my date validation script which REQUIRES that the date be in mm/dd/yyyy format.  

I would like help in creating a function that will ONBLUR check to see if the date is mm/dd/yy and if so add the appropriate 19 or 20 in front of the 2 digit year so that my validation will work properly.  If its already mm/dd/yyyy then do nothing.

Thanks in advance for your help.
Clint...
Avatar of netsmithcentral
netsmithcentral
Flag of United States of America image

No problem.  Here's the function you would want to register to the onblur event:

function yrChk(){
 var date = this.value;
 var yr = date.split('/')[2];
 if(yr.length==2) yr = (yr>50) ? '19'+yr : '20'+yr;
 date = date.split('/'); date[2] = yr; date.join('/');
 this.value = date;
}
 
Avatar of clintnash

ASKER

Great, with one small problem.  I attached your script and pass it the control which does change the two digit date to a four digit date, however it produces a strange result.

I enter in 11/11/80 and tab off, it changes the value to

11,11,1980

Any idea on how best to change those commas back to slashes?

thanks for your help,
Clint...
ASKER CERTIFIED SOLUTION
Avatar of netsmithcentral
netsmithcentral
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That fixed it perfectly.  Its converting to 4 digits and now validating the input.

Thanks so very much for your time today,
Clint...