Calculate the difference between current date and age from personal ID The goal is to show message for users that are less 25 years old. The personal ID consists of 081010333333, where 08year, 10month, 10date. I made code and it's very messy. So the question is: how to make it "right"? any advices and solutions are welcome. One on codepen and the code itself:



$('input').on('change keyup', function(i, el){
    var d = new Date(),
            getVal = $(this).val(),         
            getYear = $(this).val().slice(0,2),
            getMonth = $(this).val().slice(2,4),
            getDay = $(this).val().slice(4,6),
            getCurrentYearFull = d.getFullYear(),
            getCurrentMonth = ("0" + (d.getMonth() + 1)).slice(-2),
            getCurrentDate = ("0" + (d.getDate())).slice(-2),
            getCurrentYearShort = d.getFullYear().toString().slice(2,4);

        if(getYear &gt; getCurrentYearShort){
                getYear = "19" + getYear
            } else {
                getYear = "20" + getYear
            }
            var yearDiff = getCurrentYearFull - getYear;
            if (yearDiff &gt;=10 &amp;&amp; $('input').val().length &gt;= 4) {
                    if(getCurrentMonth &gt;= getMonth){
                         if (getCurrentDate &gt;= getDay){
                             $(".check").text('good')
                         }
                        else {
                            $(".check").text('bad')
                        }
                    } else {
                        $(".check").text('bad')
                    }
            } else {
                $(".check").text('bad')

            }
        $('.test').text(yearDiff)       
})
&lt;script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.2.2/jquery.min.js"&gt;&lt;/script&gt;   

&lt;input type="text" value="120330"/&gt;
&lt;div class="check"&gt;&lt;/div&gt;
&lt;div class="test"&gt;&lt;/div&gt;
In this answer I'll just reveiw the way you calculate the age difference.
So first, I would like to mention, that you should really work with four-digit numbers for displaing years - if that's possible for you.

In the suggestion below you'll see, that I don't compare the years, months and days because comparing two timestamps is better!
So you just have to feed the is25YearsOld function with a yyyymmdd-date and it will tell if you are older/younger than 25.



function is25YearsOld(input) {
    // the date of birth of a 25 year old
    var dob25 = new Date();
    // setFullYear to the current year (eg. 2016) and subtract 25
    dob25.setFullYear(dob25.getFullYear() - 25);
    // set the time to midnight
    dob25.setHours(0);
    dob25.setMinutes(0);
    dob25.setSeconds(0);
    // get the unix-timestamp
    dob25 = dob25.getTime();
    // the user's date of birth
    var dob = new Date();
    // set the year, month and day accordingly to the input
    dob.setFullYear(parseInt(input.substr(0, 4)));
    dob.setMonth(parseInt(input.substr(4, 2)) - 1); // (-1) because month is zero-based
    dob.setDate(parseInt(input.substr(6, 2)));
    // set the time to midnight
    dob.setHours(0);
    dob.setMinutes(0);
    dob.setSeconds(0);
    // get the unix-timestamp
    dob = dob.getTime();
    // if the timestamp of requiredDateOfBirth is smaller than the one of dateOfBirth, the user is too young
    return dob25 &gt;= dob;
}

// Oct 10, 2008
document.getElementById("test1").innerHTML = is25YearsOld("20081010") ? "You are old enough!" : "You are too young!";
// Oct 10, 1988
document.getElementById("test2").innerHTML = is25YearsOld("19881010") ? "You are old enough!" : "You are too young!";
&lt;p&gt;2008-10-10: &lt;span id="test1"&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;1988-10-10: &lt;span id="test2"&gt;&lt;/span&gt;&lt;/p&gt;