afrocave

Show Age from Date of Birth

Oct 24th, 2020
709
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Wrap in <script>// code goes here </script> tags if necessary
  2. function _calculateAge(birthday) { // birthday is a date
  3.     var ageDifMs = Date.now() - birthday.getTime();
  4.     var ageDate = new Date(ageDifMs); // miliseconds from epoch
  5.     return Math.abs(ageDate.getUTCFullYear() - 1970);
  6. }
  7.  
  8. const dateOfBirth = new Date('11/15/1995');
  9. console.log(_calculateAge(dateOfBirth) + " years old");
RAW Paste Data