validate age javascript

Solutions on MaxInterview for validate age javascript by the best coders in the world

showing results for - "validate age javascript"
Johanna
15 Mar 2020
1function isOver18(dateOfBirth) {
2  // find the date 18 years ago
3  const date18YrsAgo = new Date();
4  date18YrsAgo.setFullYear(date18YrsAgo.getFullYear() - 18);
5  // check if the date of birth is before that date
6  return dateOfBirth <= date18YrsAgo;
7}
8
9isOver18(new Date("1999-12-01")); // true
10isOver18(new Date("2020-03-27")); // false