JSFiddle - React, Tailwind, and code Playground
by Mark McFadden
HTML
<div id="daysAlive"></div>
<div id="yearsAlive"></div>
JavaScript
function Person(name, birthdate, weight) {
this.name = name;
this.birthdate = birthdate;
this.weight = weight;
}
Person.prototype = {
constructor: Person,
getDaysAlive: function () {
var birthDay = this.birthdate;
return Math.floor((new Date() - birthDay) / 86400000);
},
getYearsAlive: function () {
return (this.getDaysAlive() / 365).toFixed(2);
}
}
var markBirthDay = new Date(1961, 4, 5);
var mark = new Person("Mark", markBirthDay, 162);
console.log(mark.getDaysAlive());
console.log(mark.getYearsAlive());
$("#daysAlive").text("Days that " + mark.name + " is alive: " + mark.getDaysAlive());
$("#yearsAlive").text("Years that " + mark.name + " is alive: " + mark.getYearsAlive());