Expand my Community achievements bar.

SOLVED

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

Avatar

Level 2

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

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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=al... 

 

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

View solution in original post

8 Replies

Avatar

Correct answer by
Community Advisor

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=al... 

 

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

Avatar

Level 2

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

Avatar

Community Advisor

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

Avatar

Employee Advisor

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

Avatar

Level 2

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