I'm fairly new to JavaScript but I'm trying to calculate a difference in hours worked in a PDF form | Community
Skip to main content
Level 2
April 20, 2023
Solved

I'm fairly new to JavaScript but I'm trying to calculate a difference in hours worked in a PDF form

  • April 20, 2023
  • 6 replies
  • 2177 views

This is the code ive put together so far. I am running into the error that event.value is NOT a function.

i would appreciate any help to correct the code. This code is for an editable pdf that is in a flowed container. The table has new instance added with the click of a button so the code copies down with each new addition to the table.

Code is Below:

 

if (this.getField("timeout").valueAsString.length === 0 || this.getField("timein").valueAsString.length === 0) {
event.value = "0";
}
else{

var timefinished = this.getField("timeout").valueAsString.length;
var timestarted = this.getField("timein").valueAsString.length;

var difflnMilliSeconds = Math.abs(timefinished - timestarted )/1000;

// calculate hours
var hours = Math.floor(difflnMilliSeconds / 3600) % 24;
difflnMilliSeconds = hours *3600;

// calculate minutes
var minutes = Math.floor(difflnMilliSeconds / 60) % 60;
difflnMilliSeconds = minutes * 60;

// set field value to the difference
event.value = hours + ":" + minutes;
}

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by arunpatidar

Hi @dgarner2110 

@saravanan_dharmaraj  meant to ask question in PDF community to get better/faster response to your question

https://community.adobe.com/t5/acrobat/ct-p/ct-acrobat?page=1&sort=latest_replies&filter=all&lang=all&tabid=discussions 

 

However, i am not an expert in PDF but the javascript code to find difference between two time is

// Get the values of the two date fields var date1 = this.getField("dateField1").value; var date2 = this.getField("dateField2").value; // Calculate the difference in hours between the two dates var diffInMilliseconds = Math.abs(date2.getTime() - date1.getTime()); var diffInHours = Math.floor(diffInMilliseconds / (1000 * 60 * 60)); // Set the value of the current field to the calculated difference in hours event.value = diffInHours;

 

6 replies

Level 2
April 20, 2023

@arunpatidar do you have any experience with this?

Saravanan_Dharmaraj
Community Advisor
Community Advisor
April 20, 2023
Level 2
April 20, 2023

Ok Thanks

arunpatidar
Community Advisor
arunpatidarCommunity AdvisorAccepted solution
Community Advisor
April 20, 2023

Hi @dgarner2110 

@saravanan_dharmaraj  meant to ask question in PDF community to get better/faster response to your question

https://community.adobe.com/t5/acrobat/ct-p/ct-acrobat?page=1&sort=latest_replies&filter=all&lang=all&tabid=discussions 

 

However, i am not an expert in PDF but the javascript code to find difference between two time is

// Get the values of the two date fields var date1 = this.getField("dateField1").value; var date2 = this.getField("dateField2").value; // Calculate the difference in hours between the two dates var diffInMilliseconds = Math.abs(date2.getTime() - date1.getTime()); var diffInHours = Math.floor(diffInMilliseconds / (1000 * 60 * 60)); // Set the value of the current field to the calculated difference in hours event.value = diffInHours;

 

Arun Patidar
Level 2
April 24, 2023

The code you have shown doesn't work. the code I'm trying to figure out is for a  LiveCycle form.

arunpatidar
Community Advisor
Community Advisor
April 25, 2023

Hi @dgarner2110 
That was. a sample code, I am not an LiveCycle developer, so can't help with the code but As i suggest to move this thread to dedicated LiveCycle forum.

Arun Patidar
ManviSharma
Adobe Employee
Adobe Employee
April 22, 2023

Hi,

 

It looks like the issue with your code is that you're trying to call the value() function on the event object, which isn't a valid function.

In JavaScript, the event object doesn't have a value() function, so you'll need to use a different approach to set the field value. Instead, you can use the value property of the field object to set its value.

 

Regards,

Manvi Sharma

Level 2
April 24, 2023

I have been playing with this code:

However it returns 0 like it should, but the time calculation doesn't seem to work.

 

if (timein.rawValue == 0 || timein.rawValue == null || timeout.rawValue == 0 || timeout.rawValue == null){
this.rawValue=0;
}
else{

var timefinished = (timeout.rawValue);
var timestarted = (timein.rawValue);

var difflnMilliSeconds = Math.abs(timefinished - timestarted )/1000;

// calculate hours
var hours = Math.floor(difflnMilliSeconds / 3600) % 24;
difflnMilliSeconds = hours *3600;

// calculate minutes
var minutes = Math.floor(difflnMilliSeconds / 60) % 60;
difflnMilliSeconds = minutes * 60;

// set field value to the difference
this.rawValue = hours + "." + minutes;
}