0

Doesn't work correctly; always outputs "above 16". I have been tweeking the if statement but couldn't figure it out. Need some help, Thanks!

function hasDateCheck() {
  let dateSpliter = document.querySelector("#dateOfBirth").value.split("-");
  console.log(`${dateSpliter[0]} ${dateSpliter[1]} ${dateSpliter[2]}`); // YYYY MM DD

  let today = new Date();
  let dd = today.getDate();
  let mm = today.getMonth();
  mm++;
  let yyyy = today.getFullYear();

  if (yyyy - dateSpliter[0] < 16 && mm - dateSpliter[1] < 0 && dd - dateSpliter[2] < 0) {
    console.log("below 16");
  } else {
    console.log("above 16");
  }
}

document.querySelector("#submit").addEventListener("click", (e) => {
  e.preventDefault();
  hasDateCheck();
});
<input name="dateOfBirth" id="dateOfBirth" type="date" required/>
<input type="submit" id="submit" value="Submit" id="formSubmit">
Barmar
  • 741,623
  • 53
  • 500
  • 612
Nirzar
  • 38
  • 7

5 Answers5

1

your if condition is invalid,

replace your if condition with this,

if (yyyy - dateSpliter[0] <= 16 && mm - dateSpliter[1] <= 0 && dd - dateSpliter[2] <= 0) {
   console.log("below 16");
} else {
   console.log("above 16");
}

if you want to calculate age, then refer @Aniket answer

Sagar Davara
  • 408
  • 7
  • 13
  • That's still wrong. You shouldn't compare the month and day unless the year is exactly 16 years ago. – Barmar Feb 11 '21 at 07:25
0

Use this function to calculate age.

function age(birthday)
{
  birthday = new Date(birthday);
  return new Number((new Date().getTime() - birthday.getTime()) / 31536000000).toFixed(0);
}
Aniket
  • 391
  • 5
  • 13
0

Just use momentjs if you can import a package

var a = moment([2007, 0, 29]);
var b = moment([2007, 0, 28]);
a.diff(b, 'years')

momentjs docs

HexaCrop
  • 3,863
  • 2
  • 23
  • 50
0
if (yyyy - dateSpliter[0] > 16) {
        console.log("above 16");
    } else if (
        yyyy - dateSpliter[0] == 16 &&
        mm - dateSpliter[1] >= 0 &&
        dd - dateSpliter[2] >= 0) 
    {
        console.log("above 16");
    } else {
        guardianDetailsCheck();
        console.log("below 16");
    }
}
Nirzar
  • 38
  • 7
0

Your condition is incorrect, because, if today's date was January 15th, 2021, and you entered January 1st, 2000, it would reject it, because the day is smaller, even though the year is greater by more than 16.

The easiest way (with the less caveats) it to convert it to a Unix timestamp (a number representing the milliseconds passed since January 1th, 1970), and compare that way:

function hasDateCheck() {
  const [YYYY, MM, DD] = document.querySelector("#dateOfBirth").value.split("-");
  const birthDate = new Date(+YYYY, +MM - 1, +DD)

  //                          year day   hour min  sec  millisec
  if(Date.now() - birthDate < 16 * 365 * 24 * 60 * 60 * 1000) {
    console.log("below 16");
  } else {
    console.log("above 16");
  }
}

document.querySelector("#submit").addEventListener("click", (e) => {
  e.preventDefault();
  hasDateCheck();
});
<input name="dateOfBirth" id="dateOfBirth" type="date" required/>
<input type="submit" id="submit" value="Submit" id="formSubmit">

You might run into issues because of leap years (for each leap year, +/- 1 day inaccuracy). You may ignore this problem (few days usually don't cause problems), implement a more complex checking behavior, or reduce the inaccuracy by adding 4 days to the date (there are approximately 16 / 4 = 4 leap years in a 16-year range):

function hasDateCheck() {
  const [YYYY, MM, DD] = document.querySelector("#dateOfBirth").value.split("-");
  const birthDate = new Date(+YYYY, +MM - 1, +DD)

  //                           year day        hour min  sec  millisec
  if(Date.now() - birthDate < (16 * 365 + 4) * 24 * 60 * 60 * 1000) {
    console.log("below 16");
  } else {
    console.log("above 16");
  }
}

document.querySelector("#submit").addEventListener("click", (e) => {
  e.preventDefault();
  hasDateCheck();
});
<input name="dateOfBirth" id="dateOfBirth" type="date" required/>
<input type="submit" id="submit" value="Submit" id="formSubmit">
FZs
  • 16,581
  • 13
  • 41
  • 50