Design comparison
Solution retrospective
I struggled a lot with getting the correct time passed since a date because I didn't know what the algorithm should be like, so in the end I chose to count the days between the two dates and divide them by 365.25 to get the years etc. What do you think was the correct approach? Also, I struggled with the 'years', 'months' and 'days' elements in HTML and CSS due to their big font size (they were overflowing). How should I do it in a better way?
Community feedback
- @johnnygerardPosted 9 months ago
The simplest way to get the age is to compute a date difference between today and the birthdate.
Let me give you an example:
Pretend today is 2024-01-23 and the user's birthdate is 2000-02-28.
Compute the year difference: 2024 - 2000 = 24. It will never be negative as long as the birthdate is in the past.
Compute the month difference: 1 - 2 = -1. Because we have a negative value, we need to borrow a year and add 12 months.
Currently the computed age is: 23 years, 11 months and ? days.
Let us now compute the day difference: 23 - 28 = -5. Again a negative value, let us borrow a month and add the number of days in the previous month (December 2023).
When borrowing a month, we need to check for a negative value and borrow a year and add 12 months again in that case.
To get the number of days in the previous month, we can use this JavaScript expression:
new Date(2024, 0, 0).getDate()
. Note that the second argument (month) is zero-based and the third argument (day) is one-based. By using 0 instead of 1 for the third argument, we get the last day of the previous month instead of the first day of the current month.We can now add 31 to -5 which is 26. The computed age is 23 years, 10 months and 26 days.
We can confirm this by adding the values to the birthdate.
Birthdate: 2000-02-28
+23 years: 2023-02-28
+10 months: 2023-12-28
+26 days: 2023-12-54
We can evaluate
new Date(2023, 11, 54).toDateString()
which gives'Tue Jan 23 2024'
.Here my TypeScript code for this:
static computeAge(year: number, month: number, day: number): Age { const today = new Date; const currentYear = today.getFullYear(); const currentMonth = today.getMonth(); const currentDay = today.getDate(); let years = currentYear - year; let months = currentMonth - month; let days = currentDay - day; if (months < 0) { // Borrow a year years--; months += 12; } if (days < 0) { // Borrow a month months--; days += this.getPreviousMonthDays(currentYear, currentMonth); if (months < 0) { // Borrow a year years--; months += 12; } } return { years, months, days }; } /** * @param month Zero-based month * @param year Year * @returns Total number of days in the previous month */ private static getPreviousMonthDays(year: number, month: number): number { // Day 0 is shifted to the previous month's last day return new Date(year, month, 0).getDate(); }
Marked as helpful1@JanAbePosted 9 months ago@johnnygerard
Ty for your explanation, it helped me understand how to do it manually. I resorted to using a 3rd party library to handle dates. So this was very insightful.
1@LucaSofroniePosted 9 months ago@johnnygerard
Thank you for your suggestion! I'll surely try it and come back with the updated code.
0
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