# Calculate age in year, month, days with Java Script - three different fields

Community Beginner ,
Apr 09, 2021 Apr 09, 2021

Copied

Hello everyone, I have a question about automatic age calculation with Java Script. I've already searched the entire forum, but unfortunately nothing really fits my case.

Enclosed you will find a picture with a table. Since it is in German, here the terms line by line from left to right: year, month, day. And on the left (from top to bottom) test date, date of birth, age. I would like the client to first enter the test date in the first line, spread over three fields, e.g. 2021 - 04 - 09 and below the date of birth, e.g. 1990 - 11 - 17.

How do I manage that the age is automatically calculated in years, months and days, but distributed over the three fields? Leap years must also be taken into account, I guess? A simple subtraction doesn't work either, of course, because both, the value of the month and the day, could be higher and somehow the total number of months and days must also play a role, right?

As you can see, I am a Java Script beginner and would be grateful for any help!

TOPICS
JavaScript

Views

168

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
6 Replies 6
Most Valuable Participant ,
Apr 09, 2021 Apr 09, 2021

Copied

This is not a simple calculation, and the first step has to be to define how it should work.

What is the difference in months represent, exactly? Calendar months? If so, that would mean the results would not be consistent. For example, there are fewer days between the 1st of February and the 1st of March than between the 1st of March first and the 1st of April, yet the result for both would be 1 month and zero days, if you count it by calendar months...

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Community Beginner ,
Apr 12, 2021 Apr 12, 2021

Copied

Yes, so it's about specifying the age of the client in years, months and days. Not in the sense that e.g. 7 years correspond to 84 months and 2555 days, but that I would like to have calculated how many years, months and days old the client is exactly on the test date. In my research so far, I have come across "if" cases. Would it be possible to get it done with that?

So, e.g. "if the age-in-days-field exceeds 30, the value 1 must be added to the age-in-months-field". Same thing with the value 12 for the months. But I understand the problem ... The months have different numbers of days ... That makes the whole thing very complicated. Is there still a conceivable possibility? Or would it help if the two dates were not divided into three fields each but into one?

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Apr 12, 2021 Apr 12, 2021

Copied

What is the name of the fields?

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Community Beginner ,
Apr 12, 2021 Apr 12, 2021

Copied

I have now written the English terms next to it - see appendix. Is that helpful?

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Apr 12, 2021 Apr 12, 2021

Copied

LATEST

I mean what are names of fields where dates go? like this:

If you can,share your file or send me your file via PM if you don't want to post it publicly.

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Most Valuable Participant ,
Apr 12, 2021 Apr 12, 2021

Copied

The number of fields doesn't matter. What matters is what they represent. If you want to do it by counting a month as 30 days that's problematic too, because if someone was born on the 1st of March and today is the 31st of March that would count as one whole month, even though it's not really... Either way, you would need to make some kind of compromise.

My suggestion would be to drop the months part entirely, since it's not consistent. Use years, and if you want to get the exact difference add to it either weeks and/or days, as those have a clear numeric definition.

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Resources