Link to home
Create AccountLog in
JavaScript

JavaScript

--

Questions

--

Followers

Top Experts

Avatar of clintnash
clintnash🇺🇸

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...

Zero AI Policy

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of netsmithcentralnetsmithcentral🇺🇸

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 clintnashclintnash🇺🇸

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 netsmithcentralnetsmithcentral🇺🇸

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account

Avatar of clintnashclintnash🇺🇸

ASKER

That fixed it perfectly.  Its converting to 4 digits and now validating the input.

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

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.

JavaScript

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.