Skip to main content
toddr14813053
Inspiring
July 18, 2019
Question

Calculate age in Years/Months

  • July 18, 2019
  • 0 replies
  • 334 views

This is an extension of a previously answered question:

PREVIOUS THREAD:

I am using a calculation script to calculate age from a DOB field. It works just fine for the calculation and I can manipulate it to calc either in months or years. The problem is I want to have any ages < 1 year old to show the age in months and ages > 1 year old to show the age in years. Hopefully that makes sense. I have been searching through the existing threads and can't seem to find anything to help this specific need. If there a way to do this - maybe an additional IF clause? Here is my current calculation script.

PREVIOUS CORRECT ANSWER:

event.value = "";

var dobValue = getField("Kid.DOB").value;

if (dobValue!="") {

var dob = util.scand("mm/dd/yyyy", dobValue);

var today = new Date();

var age = today.getTime() - dob.getTime();

// compute age in milliseconds

var nAgeMilliseconds = today.getTime() - dob.getTime();

var nAgeYears = ( age / (1000 * 60 * 60 * 24 * 365.2425) - 0.005);

if(nAgeYears < 1)

{// Change to months (tricky because you have to handle new year crosing)

     var nAgeMonths = today.getMonth() - dob.getMonth();

     if(nAgeMonths < 0)

         nAgeMonths += 12;

     event.value = Math.floor(nAgeMonths);

}

else

    event.value = Math.floor(nAgeYears);

}

NEW QUESTION:

Now for an extension of the original question. For ages that are  less than 1 month, I would like the calculation to show how many weeks old they are. I have used the original script as a starting point and the script does recognize when it should be showing weeks, but there is something wrong with it actually showing the number of weeks. For example, when nAgeMonths < 1, it will change the text part of the event value from Months to Weeks, but it will show 0 weeks instead of the actually number of weeks. I hope that makes sense. I think the problem may be in one of two areas:

var nAgeWeeks = today.getMonth() - dob.getMonth(); // shouldn't this be changed since we're not using complete months?

or

nAgeWeeks += 4; //should this be something other than 4? I've tried 52 but it still showed 0 weeks at the end.

Here is the entire script to look at

event.value = "";

var dobValue = getField("Kid.DOB").value;

if (dobValue!="") {

var dob = util.scand("mm/dd/yyyy", dobValue);

var today = new Date();

var age = today.getTime() - dob.getTime();

// compute age in milliseconds

var nAgeYears = ( age / (1000 * 60 * 60 * 24 * 365.2425) - 0.005);

if(nAgeYears < 1) {

     var nAgeMonths = today.getMonth() - dob.getMonth();

     if(nAgeMonths < 0)

         nAgeMonths += 12;

     event.value = Math.floor(nAgeMonths) + " Months";

   

if(nAgeMonths < 1) {

    var nAgeWeeks = today.getMonth() - dob.getMonth();

    if(nAgeWeeks < 0)

        nAgeWeeks += 4;

    event.value = Math.floor(nAgeWeeks) + " Weeks";

    }

}

else

    event.value = Math.floor(nAgeYears) + " Years";

}

I hope all that makes sense.


Thanks, Todd

This topic has been closed for replies.