Seemingly Simple Date Calculation Creating Monstrous Headache- Help Please

Explorer ,
Mar 07, 2021 Mar 07, 2021

Copy link to clipboard

Copied

The following script is located in the custom calculation property box of the RegistrationPeriod dateObject in the attached file.  A user enters their birthdate in the CustomerDOB dateObject field, and this date entry is used in a custom calculation script in the BirthDate dateObject field where it retains the date and month values but sets the year current. For example, user-entered 11/02/1967 in the CustomerDOB dateObject field displays 11/02/2021 in the BirthDate dateObject field. In the script below, I use the value from the BirthDate dateObjectField and the user-entered date value from the DateAcquired dateObject field to calculate the RegistrationPeriod field value, which is the difference in number of months between the BirthDate dateObject field and the DateAcquired dateObject field. But it doesn't work- another set of eyes and another approach are needed- please help- thanks in advance- kemper

 

var IRP = this.getField("BirthDate").valueAsString;
var ACQ = this.getField("AcquiredDate").valueAsString;
var dIRP = util.scand("mm/dd/yyyy", IRP);
var dACQ = util.scand("mm/dd/yyyy", ACQ);
var msIRP = dIRP.getTime() - dACQ.getTime;
var oneMonth = 31 * 24 * 60 * 60 * 1000;
var months = Math.round(msIRP/oneMonth);
event.value = months;

TOPICS
How to, JavaScript

Views

70

Likes

translate

Translate

Translate

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 guidelines

correct answers 1 Correct Answer

Most Valuable Participant , Mar 07, 2021 Mar 07, 2021
Change: var msIRP = dIRP.getTime() - dACQ.getTime; To: var msIRP = dIRP.getTime() - dACQ.getTime();

Likes

translate

Translate

Translate
Adobe Community Professional ,
Mar 07, 2021 Mar 07, 2021

Copy link to clipboard

Copied

Check the Javascript console for errors.

Likes

translate

Translate

Translate

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 guidelines
Explorer ,
Mar 07, 2021 Mar 07, 2021

Copy link to clipboard

Copied


TypeError: this.getField is not a function
1:Console:Exec
undefined

ok my variable is undefined, but how does it happen this way when both dateObject fields used in the calculation have values? I believe I'm following Thom's example converting dateObjects to numerical values in milliseconds, performing the calculation, then converting the resultant value into months? I've been looking at this for awhile Bernd, I guess I have javadyslexia or something, but I'm not understanding your guidance- I apologize for my ignorance- kemper

Likes

translate

Translate

Translate

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 guidelines
Adobe Community Professional ,
Mar 07, 2021 Mar 07, 2021

Copy link to clipboard

Copied

A month is not very accurate measurement, because lenght is changed depending on what month it is, so you won't get precise calculation.
To help with your code try like this:
Change: var msIRP = dIRP.getTime() - dACQ.getTime;
with this:
var t1 = dIRP.getTime();
var t2 = dACQ.getTime();
var msIRP = t1-t2;

Likes

translate

Translate

Translate

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 guidelines
Explorer ,
Mar 07, 2021 Mar 07, 2021

Copy link to clipboard

Copied

Hi Nesa! Thank you for responding!! Your solution seems interesting and I will try tomorrow- just wanted to respond thanks b/4 too late!!!

Likes

translate

Translate

Translate

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 guidelines
Most Valuable Participant ,
Mar 07, 2021 Mar 07, 2021

Copy link to clipboard

Copied

Change:

var msIRP = dIRP.getTime() - dACQ.getTime;

To:

var msIRP = dIRP.getTime() - dACQ.getTime();

Likes

translate

Translate

Translate

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 guidelines
Explorer ,
Mar 07, 2021 Mar 07, 2021

Copy link to clipboard

Copied

embarassing! LOL!! just when you think you got it- it got you!! Thanks again try67!!! You're awesome!!!!

Likes

translate

Translate

Translate

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 guidelines
Adobe Community Professional ,
Mar 07, 2021 Mar 07, 2021

Copy link to clipboard

Copied

I think that you get undefined if a field in your variables is not spelled exactly as you named the actual field objects.

 

Your scripts works good on my end as long as you change the line in the msIRP variable from:

 

dIRP.getTime() - dACQ.getTime;

 

To :

 

dACQ.getTime() - dIRP.getTime.

 

You didn't specify exactly what do you mean by not working.

 

For example, if you substract the value of the acquired date (which based on what you've explained would be the same date but with today's current year), from the date of birth  you'll get a negative value.

 

Since you're also calculating time in miliseconds, and days in a month by 31 days , and not using the getMonth() component of the date constructor in this calculation (which in javaScript would be a value from 0 through 11 to represent a total of twelve months in a year),  it may throw off inaccurate numbers as well.

 

Not all months contain exactly 31 days. Not to mention that  not all leap years are divisible by 4, in which case , every four years we need to add one day to the month of February to make it 29 days for that leap year.

 

Such was the case with the rollover date of 1900 during the Y2K bug crisis... just to consider another scenario of possible date arithmetic errors.

 

Likes

translate

Translate

Translate

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 guidelines
Explorer ,
Mar 07, 2021 Mar 07, 2021

Copy link to clipboard

Copied

Thank you ls_rbls for your response. Yes, you are right; just a novice and needed console clarification. Also, I made a stupid mistake! Thank you ls_rbls for taking the time to help me!

Likes

translate

Translate

Translate

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 guidelines