Created
February 18, 2019 23:12
-
-
Save Virksaabnavjot/2c6d940699ae0fde2d720e0b1520a946 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Code and Explanation: https://mrvirk.com/javascript-to-calculate-age-from-date-of-birth.html |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Code and Explanation: https://mrvirk.com/javascript-to-calculate-age-from-date-of-birth.html
`//Change ME
var DOB = "March 1, 1995";
var millisecondsBetweenDOBAnd1970 = Date.parse(DOB);
var millisecondsBetweenNowAnd1970 = Date.now();
var ageInMilliseconds = millisecondsBetweenNowAnd1970-millisecondsBetweenDOBAnd1970;
var milliseconds = ageInMilliseconds;
var second = 1000;
var minute = second60;
var hour = minute60;
var day = hour24;
var month = day30;
/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 = years12;
var days = years365;
var hours = Math.round(milliseconds/hour);
var seconds = Math.round(milliseconds/second);
function printResults(){
var message = "Age in Years : "+years+
"
Age in Months : "+months+
"
Age in Days : "+days+
"
Age in Hours : "+hours+
"
Age in Seconds : "+seconds+
"
Age in Milliseconds : "+milliseconds;
document.getElementById('placeholder').innerHTML = message;
}
window.onload = printResults;`