Skip to main content
Known Participant
January 17, 2022
Answered

Calculating Total Time

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;

 

Bernd Alheit
Bernd AlheitCorrect answer
Community Expert
January 17, 2022

I figured it out, this is what it returns.

 

1970/01/0107:04


There must be a space between day and time.

18 replies

Bernd Alheit
Community Expert
Community Expert
January 17, 2022

What format does you use for start time and finished time?

Translate
BrentJMAuthor
Known Participant
January 17, 2022

Both are formatted as HH:MM

Translate
Bernd Alheit
Community Expert
Community Expert
January 17, 2022

Look what you get for
'1970/01/01' + timestarted
in the Javascript debugger.

Translate