JS Hack - Calculating Age " />
Article Image
read

JS Hack - Calculating Age

In this post I will share an extremely useful hack to apply in JavaScript. If you ever need to create a function that calculates age based on a date of birth, I recommend that you use this function that makes an extremely performative spell:


function howOld(dateString) {
  var bday = +new Date(dateString);
  return ~~((Date.now() - bday) / (31557600000));
}

howOld('06/11/1990'); //26

Blog Logo

Robson Fagundes


Published

Image

Robson Fagundes

Software Engineer - Full Stack Web Developer

Back to Overview