Expand my Community achievements bar.

I'm fairly new to JavaScript but I'm trying to calculate the difference in hours worked in a fillable 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;
}

@Vijay_Katoch  @workflowuser @Pulkit_Jain_ 

10 Replies

Avatar

Community Advisor

In which event you are calling the script? Are you setting the value for any field?

 

Avatar

Level 2

@Vijay_Katoch 

The event for the if statement is a data entry into the "timein" or "timeout" field. The calculation should run for the time when the "timeout" field data is entered. When the calculation is run it sets the value of the "total" field which has the formula in it.

Avatar

Level 2

@Vijay_Katoch 

I'm not using xfa. 

how can I attach the form for review?

This page says .pdf not supported.

Avatar

Employee Advisor

use acrobat send to send the file and attach the download link here

 

Avatar

Community Advisor

This will be your basic calculation code for hours in javascript. Hope this will help.

 

form1.Body.labor_cost.Row1.total::calculate - (JavaScript, both)

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;
this.rawValue = timefinished - timestarted;

}

Avatar

Level 2

@Vijay_Katoch 

I appreciate your help and would appreciate your help in finishing the code.

 

The code provided doesn't work with minuets just whole hours.

Is there a way to make the more complex code originally provided in the form work to display the hours in whole numbers and the minuets in a decimal?

Avatar

Level 2

I have played with it a bit and have got the if statement to return zero however the bottom half of the script won't calculate the hours.

 

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

 

 

@Vijay_Katoch 

@workflowuser 

Avatar

Community Advisor

Here you go..

 

if (timein.rawValue == 0 || timein.rawValue == null || timeout.rawValue == 0 || timeout.rawValue == null){
this.rawValue=0;
}else{
var startTime= new Date("01/01/2007 " + timein.rawValue);
var endTime= new Date("01/01/2007 " + timeout.rawValue);
var timeDiff = getTimeDifference(startTime, endTime);
this.rawValue = timeDiff
}

function getTimeDifference(startTime, endTime) {
var difference = endTime - startTime;
var differenceInMinutes = difference / 1000 / 60;
var hours = Math.floor(differenceInMinutes / 60);
if (hours < 0) {
hours = 24 + hours;
}
var minutes = Math.floor(differenceInMinutes % 60);
if (minutes < 0) {
minutes = 60 + minutes;
}
var hoursAndMinutes = hours + ":" + (minutes < 10 ? '0' : '') + minutes;
return hoursAndMinutes;
}