Skip to main content
Participating Frequently
April 9, 2021
Question

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

  • April 9, 2021
  • 1 reply
  • 2046 views

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!

This topic has been closed for replies.

1 reply

try67
Community Expert
Community Expert
April 9, 2021

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...

 

Alina5C99Author
Participating Frequently
April 12, 2021

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?

Nesa Nurani
Community Expert
Community Expert
April 12, 2021

What is the name of the fields?