Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Virksaabnavjot/2c6d940699ae0fde2d720e0b1520a946 to your computer and use it in GitHub Desktop.
Save Virksaabnavjot/2c6d940699ae0fde2d720e0b1520a946 to your computer and use it in GitHub Desktop.
Code and Explanation: https://mrvirk.com/javascript-to-calculate-age-from-date-of-birth.html
@Virksaabnavjot
Copy link
Author

Virksaabnavjot commented Feb 18, 2019

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 = minute
60;
var day = hour24;
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 = years12;
var days = years
365;
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;`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment