Submitted 10 months ago
Age Calculator using HTML, Tailwind and JS Form Validation
@GreenCitrus6
Design comparison
SolutionDesign
Solution retrospective
I struggled with getting the calculated age to be consistent across different lengths of time. The approach I took was to:
- Fetch the current time as a millisecond time stamp
- Convert the user inputted date to a millisecond time stamp
- Find the difference between the two time stamps
- Convert that difference into a number of years by dividing by the average number of milliseconds in a year
- Convert the remainder after calculating years into months by dividing by the average number of milliseconds in a month
- Convert the remainder after calculating months into days by dividing by the number of milliseconds in a day
- Use Math.floor() to round years, months and days down
The code for the calculation is as follows:
function timestampToYMD(timestampDiff) {
// calculated age is not accurate, try converting from ms to days first, then to months and years?
const MsInADay = (1000 * 60 * 60 * 24)
const NumOfLeapYears = (1000/4) - 7;
const ExactMonthLength =
// Average length of a month, accounting for leap years using the number of leap years between 1000 and 2000
(((1000 - ((1000/4) - 7)) * ((31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31) / 12) //average month length in a normal year
+
(((1000/4) - 7) * (31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31) / 12) ) //average month length in a leap year
/ 1000 /* averaging out the normal year month length and leap year month length over the span of 1000 years */);
const MsInAYear = (
// taking years of 1001 to 2000
/* LEAP YEAR RULES:
Divisible by 4
For centuries, those divisible by 400
*/
(365 + (NumOfLeapYears/ 999)) * MsInADay
);
// 31_557_016_216.21622
let numOfYears = timestampDiff / MsInAYear;
let numOfMonths = ((timestampDiff % MsInAYear) / (MsInADay * ExactMonthLength));
let numOfDays = ((timestampDiff % MsInAYear) % (ExactMonthLength * MsInADay) / MsInADay);
document.querySelector("#num-of-years").innerHTML = Math.floor(numOfYears);
document.querySelector("#num-of-months").innerHTML = Math.floor(numOfMonths);
document.querySelector("#num-of-days").innerHTML = Math.floor(numOfDays);
}
This approach has a margin of error of a few days, so I'm wondering if there is a better approach to make the calculation more accurate. Would it be better to actually find the distance in days between two calendar dates than to calculate it using milliseconds?
Community feedback
Please log in to post a comment
Log in with GitHubJoin our Discord community
Join thousands of Frontend Mentor community members taking the challenges, sharing resources, helping each other, and chatting about all things front-end!
Join our Discord