Skip to main content
Participating Frequently
August 7, 2022
Question

Subtract M/D/YYYY HH:MM from M/D/YYYY HH:MM

  • August 7, 2022
  • 1 reply
  • 1261 views

I am trying to subtract 2 dates to determine the amount of time between them.

Both dates are formatted M/D/YYYY HH:MM

date1 is a user input date field called subjectStart.

date2 is a user input date field called canineStart. Date2 will always be a later date than date1.

The resulting information will be displayed into a field called "age". Not sure if it needs to be a text field or a date field?

Any assistance would be GREATLY appreciated!

This topic has been closed for replies.

1 reply

Participating Frequently
August 7, 2022

This is my current attempt...

// Get the input field values
var s1 = getField("date4_af_date").valueAsString;
var s2 = getField("date6_af_date").valueAsString;
var t1 = util.scand("HH-MM", s1);
var t2 = util.scand("HH-MM", s2);

if (s1 && s2) {// Convert date strings to date objects
var d1 = util.scand("dd-mmm-yy", s1);
var d2 = util.scand("dd-mmm-yy", s2);
var h1 = util.scand("HH", s1);
var m1 = util.scand("HH", s1);
var h2 = util.scand("HH", s2);
var m2 = util.scand("HH", s2);

//Hours/minutes to milliseconds from s1
var h3 = Math.abs(Math.round(h1 * 36000000));
var m3 = Math.abs(Math.round(m1 * 60000));

//Hours/minutes to milliseconds from s2
var h4 = Math.abs(Math.round(h2 * 36e6));
var m4 = Math.abs(Math.round(m2 * 60000));

//Time Difference in Milliseconds S2-S1
var mT = Math.abs(Math.round((h2 + m2) - (h1 + m1)));

//Date Difference in Milliseconds
var d5 = Math.abs(Math.round(d2-d1));

//Date Time Difference in Milliseconds
var dmT = Math.abs(Math.round(D5-mT));

// Set this field value to the difference in days
// 864e5 = 86400000 = 1000 * 60 * 60 * 24 = number of milliseconds in a day

//Math Displayed
event.value = Math.abs(Math.round(dmT/864e5));

} else {
// Alert user
event.value = "Enter both dates";
}

ls_rbls
Community Expert
Community Expert
August 7, 2022

In my humble opinion, this may be done a lot more easier using other methods to calculate difference between months and time.

 

The script that you shared seem like it was copied and pasted from another source.

 

There are errors in how you declared some of the variables as well.

 

For starters, I would consider using "this.getField" rather than "getField" to declare the main two variable that get the string value from the two date fields.

 

See here:

 

 

Also, in my forms I don't let Acrobat assign the suffix of "af_date". I eliminate that part in my date field names.

 

I also take care in renaming them with a unique date filed name that is different from other parent field names, such those that are assigned to other field objects or  widgets (radio buttons, checkboxes), dropdown menus, list boxes, texfields.

 

And While some people may disagree with that, I've experienced issues in tge past withscripts when date fields are appended with  "af_date" to their field name.

 

Anyway, lets see this part:

 

"var h1 = util.scand("HH", s1);
var m1 = util.scand("HH", s1);

"

Can you spot the error in these two lines? 

 

Variable "h1" declaration 

employs the util.scand() method to get the value for hours while "m1" should be for minutes, correct?

 

Also,  you have a variable named "d5" that is used later in that script as a condition, yet it is spelled as "D5"(in upper case); seems like a typo. Nevertheless, variable names, to include whatever is declared in them should match exact object names and proper syntax.

 

In addition, Calculating dates, specifically when difference in time is calculated, the scripting becomes very tricky.

 

Extra effort must be invested in reading and understaning how Acrobat JavaScript works around this type of arithmetic.

 

See the discussion linked below for additional insights:

 

 

That said, never express dates in upper case letters

"M/D/YYYY" since letter "M"

in upper case will be interpreted by the core JavaScript as minutes not months (and throw errors).

 

Can you please also verify what errors are you getting in the JavaScript console and post them here in the forum to get better assisted.

Participating Frequently
August 8, 2022

I noticed those errors right after I hit submit on the post! lol Thank you for the information & explaining it!