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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • neelsfer
    Contributor
    • Oct 2010
    • 547

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

    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
    Code:
    	<form>
    		<input type="text" name="idno" id="idno" value="" placeholder="ID Number" onkeyup="dateOfBirthsum();" />
    		<input type="text" name="dob" id="dob"  placeholder="birthdate"/>
    		
    	</form>
    javascript
    Code:
    <script>
    	
    function dateOfBirthsum()
    {    var idnumInput = document.getElementById('idno').value;
    
    	var dateDay = idnumInput.substring(4,6);
    	var dateMonth = idnumInput.substring(2,4);
    	var dateYear = idnumInput.substring(0,2);
       var result = dateDay + "/" + dateMonth + "/" + dateYear;
    	document.getElementById('dob').value = result;
    	}
    	</script>

    I got this code somewhere but cannot add it
    Code:
    var year      	= id_number.substr ( 0  , 2 );
    					var nowYearNotCentury = currentTime.getFullYear() + '';
    					nowYearNotCentury = nowYearNotCentury.substr(2,2);
    					if (year <= nowYearNotCentury){
    						date = '20' + year+ "-" + id_number.substr(2, 2) + "-" + id_number.substr(4, 2);
    					} else {
    						date = '19' + year+ "-" + id_number.substr(2, 2) + "-" + id_number.substr(4, 2);
    					}//end if
  • neelsfer
    Contributor
    • Oct 2010
    • 547

    #2
    After 3 hours of struggling, i got it working. Anybody else needing a very basic solution, here it is
    Code:
     <body>
    	<form>
    		<input type="text" name="idno" id="idno" value="" placeholder="ID Number" onkeyup="dateOfBirthsum();" />
    		
    		<input type="text" name="dob2" id="dob2"  placeholder="birthdate2"/>
    	</form>
    <script>
    	
    function dateOfBirthsum()
    {    var idnumInput = document.getElementById('idno').value;
    
    	var dateDay = idnumInput.substring(4,6);
    	var dateMonth = idnumInput.substring(2,4);
    	var dateYear = idnumInput.substring(0,2);
    
    	var dateYear1 = '19' + dateYear;
        var dateYear2 = '20' + dateYear;
    	var Year1 = dateYear;
        var Year2 = dateYear;
    	var years = new Date();
    	var dateYear3 = years.getFullYear();
    	var dateYear3a = idnumInput.substring(0,2);
    	
      if (dateYear1 < dateYear3a){
    			result3	= dateDay + "/" + dateMonth + "/" + '19' + dateYear;
    					} else {
    						result3	= dateDay + "/" + dateMonth + "/" + '20' + dateYear;
    					}
    	
    	document.getElementById('dob2').value = result3;
    	
    	}
    	</script>

    Comment

    • Dormilich
      Recognized Expert Expert
      • Aug 2008
      • 8694

      #3
      out of curiousity, how do you determine the DoB for people older than 100 years?

      Comment

      • neelsfer
        Contributor
        • Oct 2010
        • 547

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

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #5
          OK. please keep us updated, if they do.

          Comment

          Working...