Skip to content

Instantly share code, notes, and snippets.

@Sammuel09
Last active January 25, 2021 21:37
Show Gist options
  • Save Sammuel09/66c7a2b157b91c30cd7f23acc556a52d to your computer and use it in GitHub Desktop.
Save Sammuel09/66c7a2b157b91c30cd7f23acc556a52d to your computer and use it in GitHub Desktop.
function validatateAgeBelow18 (value){
// value is the user chosen date
// get today's date
var todaysDate = Date.now()
var todaysDateInSeconds = Math.floor(todaysDate/1000);
console.log("todaysdate", todaysDateInSeconds);
// convert to epoch time
// comvert to seconds.
// get date user choose
var userDate = value;
var userDateInEpochTime = Date.parse(userDate);
// console.log("userdate", userDateInEpochTime);
var userDateInEpochTimeInSeconds = Math.floor(userDateInEpochTime/1000);
console.log("userdate", userDateInEpochTimeInSeconds);
// console.log("userdate", userDateInEpochTime);
// convert to epoch time and seconds
// subtract user date from todays date
var diffInAge = todaysDateInSeconds - userDateInEpochTimeInSeconds;
console.log("diffInage", diffInAge );
var _18YearsInS = 567648000;
// check if it is less than 18 years.
var isUserOlder18 = diffInAge > _18YearsInS
console.log(isUserOlder18)
// if so, return an error that the age cannot be less 18
// if above 18, the validator returns no erros
}
validateAgeBelow18(2021-01-05)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment