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

New Here ,
Aug 07, 2022 Aug 07, 2022

Copy link to clipboard

Copied

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!

TOPICS
How to , JavaScript , PDF forms

Views

89

Likes

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
New Here ,
Aug 07, 2022 Aug 07, 2022

Copy link to clipboard

Copied

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";
}

Likes

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
Community Expert ,
Aug 07, 2022 Aug 07, 2022

Copy link to clipboard

Copied

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.

Likes

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
New Here ,
Aug 08, 2022 Aug 08, 2022

Copy link to clipboard

Copied

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

Likes

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
Community Expert ,
Aug 07, 2022 Aug 07, 2022

Copy link to clipboard

Copied

Search the forum for "age calculation" or "calcAge". This issue was discussed many times in the past, and complete code examples were given on how to do it. Basically, you just need to use the getTime method on both Date objects, and then deduct one value from another to get the result (in milliseconds). Then convert that to years.

Or you can deduct the years, then examine the month and day to see if an additional year needs to be added to the result.

Likes

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
New Here ,
Aug 07, 2022 Aug 07, 2022

Copy link to clipboard

Copied

You guys are right I was making this way harder than it is. Thank you for the links they were super helpful!
My current:

//Custom calculation script to calculate Age based on date of birth and date of death

var subjectPlacedd= this.getField("SubjectDay/TimePlaced").value;
var k9Startd = this.getField("K9StartD/T").value;

if (k9Startd && subjectPlacedd) {

var k9Startstr = util.scand("m-d-yyyy hh-mm", k9Startd);

var subjectPlacedstr = util.scand("m-d-yyyy hh-mm", subjectPlacedd);

//calculate the difference in miliseconds between the death date and birth date

var diff = (k9Startstr.valueOf() - subjectPlacedstr.valueOf())/1000;

// calculate minutes (in years currently)

var ageMin = (((diff / 60) / 60 / 24 / 365));

//round off the total using util.printf() method

event.value = util.printf("%f", ageMin);

}
Script is currently working, but I need to find a different way to display the result ("%f", ageMin) 
Is there a way to display it as (d " day(s)" hh:mm) Something like:  1 " day(s)" 00:48

That is my next issue to work on.

Likes

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
New Here ,
Aug 08, 2022 Aug 08, 2022

Copy link to clipboard

Copied

A bit stuck again... Can you explain this error?

 

This is resulting in "SyntaxError: invalid XML name 19: at line 20"

 

//Custom calculation script to calculate Age based on date of CanineStart and SubjectStart

var subjectPlacedd= this.getField("SubjectDay/TimePlaced").value;
var k9Startd = this.getField("K9StartD/T").value;
var age = this.getField("age").value;

if (k9Startd && subjectPlacedd) {

var k9Startstr = util.scand("m-d-yyyy hh-mm", k9Startd);

var subjectPlacedstr = util.scand("m-d-yyyy hh-mm", subjectPlacedd);

//calculate the difference in miliseconds between the CanineStart and SubjectStart

var diff = (k9Startstr.valueOf() - subjectPlacedstr.valueOf())/1000;

// calculate minutes with hours & days = 0 & display (ageMin " minute(s)")

if (diff >= 60000 && < 3.6e+6){
var ageMin = (diff / 60 / 60);
var age = (ageMin + " min(s)");
}}

Likes

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
New Here ,
Aug 08, 2022 Aug 08, 2022

Copy link to clipboard

Copied

Forgot the else if in the last statement. 
Missed a }
Now more errors, but also no output into the age field.

//Custom calculation script to calculate Age based on date of CanineStart and SubjectStart

var subjectPlacedd = this.getField("SubjectDay/TimePlaced").value;
var k9Startd = this.getField("K9StartD/T").value;

if (k9Startd && subjectPlacedd) {

var k9Startstr = util.scand("m-d-yyyy hh-mm", k9Startd);

var subjectPlacedstr = util.scand("m-d-yyyy hh-mm", subjectPlacedd);

//calculate the difference in miliseconds between the CanineStart and SubjectStart

var diff = (k9Startstr.valueOf() - subjectPlacedstr.valueOf())/1000;}

// calculate minutes with hours & days = 0 & display 0 days ageMin " minute(s)"

else if ((diff >= 60000) && (diff < 36000000)){
var ageMin = ((diff / 60));
var age = getField("age").value = ('ageMin' + " min(s)");
};

 

Likes

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
Community Expert ,
Aug 08, 2022 Aug 08, 2022

Copy link to clipboard

Copied

You're not applying the result back to the field. Change the last line before the closing curly brackets to:

event.value = ageMin + " min(s)";

Likes

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
Community Expert ,
Aug 08, 2022 Aug 08, 2022

Copy link to clipboard

Copied

LATEST

It would be easier if you share an example of your PDF with no sensitive data on it.

 

Your script is not working because there are still many errors in the structure and the syntax.

 

For example, look at this part:

 

hh-mm

 

to express hours and minutes you must substitute the hyphen "-" symbol with a colon symbol ":"

 

like this :  hh:MM

 

Also, the same observation that I mentioned earlier about lower or upper case applies when you are formatting a field or declaring a variable for hours and minutes.

 

"hh" in lower case is used to express time in hours but for a twelve-hour clock.

 

That said, you have to become aware that when you employ a 12-hour clock format a distinction between a.m.(am = ante meridiam) or p.m. (post meridiam) must be taken into consideration to when it comes to a time that is expressed as "12:00" 

 

is it 12:00 noon? or is it 12:00 at midnight?

 

12:00 am or 12:00 pm?

 

While "HH" denotes a 24-hour clock (also commonly referred to as military time), which can eliminate such headaches when performing date and time arithmetic with a 12-Hour clock.

 

In a 24-Hour clock time is expressed as 

 

1200 for noon (and is read as twelve hundred hours which is 12 at noon (not twelve o' clock, not twelve sharp, not lunch time).

 

 0000 humdred hours is used to express midnight respectively. 

 

However, a lot of people also express midnight as twentyfour 2400 hundred hours in military parlance.

 

In terms of JavaScript core date and time arithmetic, you may want to stick to 0000 hundred hours for the sake of simplicity  (if any of these makes sense).

 

In addition, time in Minutes can't be expressed in lower case "mm" because it will be interpreted by the core JavaScript as month, not minutes (it will throw errors).

 

You must use "MM" to express minutes.

 

Then you are also employing the syntax of the util.printf() incorrectly:

 

util.printf("%f

 

See an example in the screenshot below based on what you were asking initially:

 

 

20220808_005250.jpg

Likes

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

Make content for your business needs with Adobe Express.

Get started easily with free templates: