Sign in
Log inSign up

finding age in JavaScript

Default profile photo
Anonymous
·Jul 17, 2018
function Person(name,dob){
this.name=name;
this.birthday=new Date(dob);
this.calculateAge = function(){
const diff = Date.now()-this.birthday.getTime();
const ageDate = new Date(diff);
return Math.abs(ageDate.getUTCFullYear()-1970);
}
const brad11 = new Person('Brad','1993-08-29');

The above code is working fine the confusion here is in the line of return statement I am unable to understand the logic that why we are multiplying ageDate with getUTCFullyear and then subtracting 1970 from it .

Hassle-free blogging platform that developers and teams love.
  • Docs by Hashnode
    New
  • Blogs
  • AI Markdown Editor
  • GraphQL APIs
  • Open source Starter-kit

© Hashnode 2024 — LinearBytes Inc.

Privacy PolicyTermsCode of Conduct