Copy link to clipboard
Copied
I have the below javascript to calculate total time in a PDF form. The start time and finished time are two separate text boxes that are filled when the user pushes the approriate button for the start time or finished time or the use can manually enter the time. When the text boxes are empty, the TIME.TOTAL text box returns 0:0. If I press the start time and/or the finished time buttons, then the TIME.TOTAL text box returns NaN:NaN. I am relatively new to javascript.
var timefinished = this.getField("TIME.FINISHED").value;
var timestarted = this.getField("TIME.STARTED").value;
var datetimefinished = new Date('1970/01/01' + timefinished);
var datetimestarted = new Date('1970/01/01' + timestarted);
var difflnMilliSeconds = Math.abs(datetimefinished - datetimestarted)/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;
There must be a space between day and time.
Copy link to clipboard
Copied
What format does you use for start time and finished time?
Copy link to clipboard
Copied
Both are formatted as HH:MM
Copy link to clipboard
Copied
Look what you get for
'1970/01/01' + timestarted
in the Javascript debugger.
Copy link to clipboard
Copied
I am going into the debugger and I hit enter in that line of code, but nothing shows up in the console. I am using a Mac and I have never used the debugger before.
Copy link to clipboard
Copied
I figured it out, this is what it returns.
1970/01/0107:04
Copy link to clipboard
Copied
Time start and finish is on same day or is there possibility finish time can be on next day?
Copy link to clipboard
Copied
It will always be on the same day. I don't see any instance where the finished time would go into the next day.
Copy link to clipboard
Copied
I am working on this same problem but my workers arein the restaurant industry and could work over over the midnight hour. So I need to also include the date on the start and end times. Do you have a solution for this?
Copy link to clipboard
Copied
There must be a space between day and time.
Copy link to clipboard
Copied
That worked, thank-you for your help.
Copy link to clipboard
Copied
CAN U POST THE CORRECTED SCRIPT I'M NEW I CANT FOLLOW ALONG TO WHERE THE ERROR IS AT
Copy link to clipboard
Copied
Were you able to get the correct code withouth returning NaN:NaN?
Copy link to clipboard
Copied
Below is the correct code.
if ((this.getField("TIME.FINISHED").value.length == 0) || (this.getField("TIME.STARTED").value.length == 0)) {
event.value = " 0 Hours 0 Minutes";
}
else{
var timefinished = this.getField("TIME.FINISHED").value;
var timestarted = this.getField("TIME.STARTED").value;
var datetimefinished = new Date('1970/01/01' + " " + timefinished);
var datetimestarted = new Date('1970/01/01' + " " + timestarted);
var difflnMilliSeconds = Math.abs(datetimefinished - datetimestarted)/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 + " Hours & " + minutes + " Minutes";
}
Copy link to clipboard
Copied
How should I modify this script if I want the calculated total to be in decimal format and to deduct for lunch out and lunch in difference?
Copy link to clipboard
Copied
I know this is a couple years old, but did anyone ever figure out how to make the total in decimal format?
Copy link to clipboard
Copied
I did eventually figure it out. I have a couple of different spreadsheets I made. One where I enter the lunch in and lunch out each day and the other where I just enter an amount of time to be deducted for lunch each day. I will try to send you a message with one of them.