• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Calculating Total Time

New Here ,
Jan 17, 2022 Jan 17, 2022

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;

 

TOPICS
JavaScript , PDF forms

Views

4.6K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jan 17, 2022 Jan 17, 2022

There must be a space between day and time.

Votes

Translate

Translate
Community Expert ,
Jan 17, 2022 Jan 17, 2022

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 17, 2022 Jan 17, 2022

Copy link to clipboard

Copied

Both are formatted as HH:MM

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 17, 2022 Jan 17, 2022

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 17, 2022 Jan 17, 2022

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 17, 2022 Jan 17, 2022

Copy link to clipboard

Copied

I figured it out, this is what it returns.

 

1970/01/0107:04

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Jan 17, 2022 Jan 17, 2022

Copy link to clipboard

Copied

Time start and finish is on same day or is there possibility finish time can be on next day?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 17, 2022 Jan 17, 2022

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 21, 2022 Jul 21, 2022

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?

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 17, 2022 Jan 17, 2022

Copy link to clipboard

Copied

There must be a space between day and time.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 17, 2022 Jan 17, 2022

Copy link to clipboard

Copied

That worked, thank-you for your help.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 05, 2022 Feb 05, 2022

Copy link to clipboard

Copied

CAN U POST THE CORRECTED SCRIPT I'M NEW I CANT FOLLOW ALONG TO WHERE THE ERROR IS AT

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 24, 2022 May 24, 2022

Copy link to clipboard

Copied

Were you able to get the correct code withouth returning NaN:NaN? 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 25, 2022 May 25, 2022

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Sep 23, 2022 Sep 23, 2022

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 13, 2024 Jun 13, 2024

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? 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jun 14, 2024 Jun 14, 2024

Copy link to clipboard

Copied

LATEST

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines