JavaScript
--
Questions
--
Followers
Top Experts
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...
  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...
Zero AI Policy
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
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;
}
Â
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;
}
Â
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...
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...
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
That fixed it perfectly. Â Its converting to 4 digits and now validating the input.
Thanks so very much for your time today,
Clint...
Thanks so very much for your time today,
Clint...






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
JavaScript
--
Questions
--
Followers
Top Experts
JavaScript is a dynamic, object-based language commonly used for client-side scripting in web browsers. Recently, server side JavaScript frameworks have also emerged. JavaScript runs on nearly every operating system and in almost every mainstream web browser.