HiveBrain v1.2.0
Get Started
← Back to all entries
gotchajavascriptMinor

Calculate the difference between current date and age from personal ID

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
theagedatedifferencebetweencalculatecurrentandfrompersonal

Problem

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 > getCurrentYearShort){
getYear = "19" + getYear
} else {
getYear = "20" + getYear
}
var yearDiff = getCurrentYearFull - getYear;
if (yearDiff >=10 && $('input').val().length >= 4) {
if(getCurrentMonth >= getMonth){
if (getCurrentDate >= getDay){
$(".check").text('good')
}
else {
$(".check").text('bad')
}
} else {
$(".check").text('bad')
}
} else {
$(".check").text('bad')

}
$('.test').text(yearDiff)
})



Solution

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 >= 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!";


2008-10-10:

1988-10-10:

Context

StackExchange Code Review Q#124330, answer score: 2

Revisions (0)

No revisions yet.