My FeedDiscussionsHeadless CMS
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more

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 .