By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,983 Members | 1,717 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,983 IT Pros & Developers. It's quick & easy.

Needs dd/mm/yyyy birthdate format from a string in javascript

100+
P: 547
I need to change a South African ID number to the date of birth (DOB)dd/mm/yyyy using javascript
example of ID Number - 0203185149088.
My code gives it as 18/03/02 instead of 18/03/2002 and if ID No is 6202185155087 then i get 18/02/62 and not 18/02/1962
The years before 2000 and after needs to be added please
dd/mm/yyyy
Please assist
Expand|Select|Wrap|Line Numbers
  1.     <form>
  2.         <input type="text" name="idno" id="idno" value="" placeholder="ID Number" onkeyup="dateOfBirthsum();" />
  3.         <input type="text" name="dob" id="dob"  placeholder="birthdate"/>
  4.  
  5.     </form>
javascript
Expand|Select|Wrap|Line Numbers
  1. <script>
  2.  
  3. function dateOfBirthsum()
  4. {    var idnumInput = document.getElementById('idno').value;
  5.  
  6.     var dateDay = idnumInput.substring(4,6);
  7.     var dateMonth = idnumInput.substring(2,4);
  8.     var dateYear = idnumInput.substring(0,2);
  9.    var result = dateDay + "/" + dateMonth + "/" + dateYear;
  10.     document.getElementById('dob').value = result;
  11.     }
  12.     </script>

I got this code somewhere but cannot add it
Expand|Select|Wrap|Line Numbers
  1. var year          = id_number.substr ( 0  , 2 );
  2.                     var nowYearNotCentury = currentTime.getFullYear() + '';
  3.                     nowYearNotCentury = nowYearNotCentury.substr(2,2);
  4.                     if (year <= nowYearNotCentury){
  5.                         date = '20' + year+ "-" + id_number.substr(2, 2) + "-" + id_number.substr(4, 2);
  6.                     } else {
  7.                         date = '19' + year+ "-" + id_number.substr(2, 2) + "-" + id_number.substr(4, 2);
  8.                     }//end if
Mar 21 '17 #1
Share this Question
Share on Google+
4 Replies


100+
P: 547
After 3 hours of struggling, i got it working. Anybody else needing a very basic solution, here it is
Expand|Select|Wrap|Line Numbers
  1.  <body>
  2.     <form>
  3.         <input type="text" name="idno" id="idno" value="" placeholder="ID Number" onkeyup="dateOfBirthsum();" />
  4.  
  5.         <input type="text" name="dob2" id="dob2"  placeholder="birthdate2"/>
  6.     </form>
  7. <script>
  8.  
  9. function dateOfBirthsum()
  10. {    var idnumInput = document.getElementById('idno').value;
  11.  
  12.     var dateDay = idnumInput.substring(4,6);
  13.     var dateMonth = idnumInput.substring(2,4);
  14.     var dateYear = idnumInput.substring(0,2);
  15.  
  16.     var dateYear1 = '19' + dateYear;
  17.     var dateYear2 = '20' + dateYear;
  18.     var Year1 = dateYear;
  19.     var Year2 = dateYear;
  20.     var years = new Date();
  21.     var dateYear3 = years.getFullYear();
  22.     var dateYear3a = idnumInput.substring(0,2);
  23.  
  24.   if (dateYear1 < dateYear3a){
  25.             result3    = dateDay + "/" + dateMonth + "/" + '19' + dateYear;
  26.                     } else {
  27.                         result3    = dateDay + "/" + dateMonth + "/" + '20' + dateYear;
  28.                     }
  29.  
  30.     document.getElementById('dob2').value = result3;
  31.  
  32.     }
  33.     </script>
Mar 21 '17 #2

Dormilich
Expert Mod 5K+
P: 8,639
out of curiousity, how do you determine the DoB for people older than 100 years?
Mar 22 '17 #3

100+
P: 547
Thx for comment. It is for a cycling races online registration site. I don't expect to many 101 year old cyclist's to enter.
Mar 22 '17 #4

Dormilich
Expert Mod 5K+
P: 8,639
OK. please keep us updated, if they do.
Mar 22 '17 #5

Post your reply

Sign in to post your reply or Sign up for a free account.