- Create age.js file and fill it with this code:
age.js
<!-- CHANGE ME -->
var DOB = "August 31, 1998";
var millisecondsBetweenDOBAnd1970 = Date.parse(DOB);
var millisecondsBetweenNowAnd1970 = Date.now();
var ageInMilliseconds = millisecondsBetweenNowAnd1970-millisecondsBetweenDOBAnd1970;
//--We will leverage Date.parse and now method to calculate age in milliseconds refer here https://www.w3schools.com/jsref/jsref_parse.asp
var milliseconds = ageInMilliseconds;
var second = 1000;
var minute = second*60;
var hour = minute*60;
var day = hour*24;
var month = day*30;
//using 30 as base as months can have 28, 29, 30 or 31 days depending a month in a year it itself is a different piece of comuptation*/
var year = day*365;
//let the age conversion begin
var years = Math.round(milliseconds/year);
var months = years*12;
var days = years*365;
var hours = Math.round(milliseconds/hour);
var seconds = Math.round(milliseconds/second);
function printResults(){
var message = ""+years;
document.getElementById('age').innerHTML = message;
}
window.onload = printResults;
- Put the
age.js
script into yourindex.html
inside the body tag, before</body>
.
index.html
<html>
<head>
<title>
EXAMPLE
</title>
</head>
<body>
<!-- Start HTML Body/CSS →
Age: <span id="age"></span>
<!-- End HTML Body/CSS →
<script src="../age.js"></script>
</body>
</html>
Still confused? Feel free to contact me.