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

Timesheet help

Explorer ,
Jul 06, 2022 Jul 06, 2022

Copy link to clipboard

Copied

Hi All 

I'm trying to create an auto calculated timesheet and I got it to work if the input time starts and end on the same day using this code:

var start = this.getField("Start Time").value;
var startArr = start.split(":");
var finish = this.getField("Stop Time").value;
var finishArr = finish.split(":");
var hourDiff = Math.abs(finishArr[0] - startArr[0]);
var minDiff = Math.floor((Math.abs(finishArr[1] - startArr[1]) / 60)*100);

if (minDiff.toString().length == 1)
minDiff = '0' + minDiff;

var output = hourDiff + "." + minDiff;
event.value = output;

 

I ran into a problem where if the Stop Time happens the next day (or 3 days later, etc) how do I go about calculating that? 

TOPICS
Acrobat SDK and JavaScript

Views

72

Likes

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 , Jul 06, 2022 Jul 06, 2022

You could use the util.scand method to convert the values into Date objects. Then use the getTime method of each one of those objects to get a representation of that time as a number. Deduce the Stop Time number from the Start Time number and you got your results (in milliseconds). Then convert it to hours and you're done.

Likes

Translate

Translate
Community Expert ,
Jul 06, 2022 Jul 06, 2022

Copy link to clipboard

Copied

Add fields for the dates.

Likes

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 ,
Jul 06, 2022 Jul 06, 2022

Copy link to clipboard

Copied

The script has no way of knowing if the time elapsed between 8:00 and 9:00 is one hour, 25 hours, 49 hours, etc., unless you also specify the full date for each time.

Likes

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
Explorer ,
Jul 06, 2022 Jul 06, 2022

Copy link to clipboard

Copied

so if I add in a time and date field like this, do you have a suggestion as to how to start figuring out a way to do it? Thanks

 

getField("Start Time").value = util.printd("dd-mmm-yyyy HH:MM", new Date());

getField("Stop Time").value = util.printd("dd-mmm-yyyy HH:MM", new Date());

Likes

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 ,
Jul 06, 2022 Jul 06, 2022

Copy link to clipboard

Copied

You could use the util.scand method to convert the values into Date objects. Then use the getTime method of each one of those objects to get a representation of that time as a number. Deduce the Stop Time number from the Start Time number and you got your results (in milliseconds). Then convert it to hours and you're done.

Likes

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
Explorer ,
Jul 06, 2022 Jul 06, 2022

Copy link to clipboard

Copied

do I start it like this? (I'm reading up the date time tutorial on the forum but it's confusing)
var strStart = this.getField("Start Time").value;
var strEnd = this.getField("Stop Time").value;
if(strStart.length && strEnd.length)
{
var dateStart = util.scand("dd-mmm-yyyy HH:MM",strStart);
var dateEnd = util.scand("dd-mmm-yyyy HH:MM",strEnd);
var diff = dateEnd.getTime() - dateStart.getTime();

 

 

Likes

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 ,
Jul 06, 2022 Jul 06, 2022

Copy link to clipboard

Copied

Seems good!

Likes

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
Explorer ,
Jul 07, 2022 Jul 07, 2022

Copy link to clipboard

Copied

I was able to get it to calculate minutes so getting hours shouldn't be hard. Thanks for your help

For those that had the same problem as me:

var strStart = this.getField("Start Time").value;
var strEnd = this.getField("Stop Time").value;
if(strStart.length && strEnd.length)
{
var dateStart = util.scand("dd-mmm-yyyy HH:MM",strStart);
var dateEnd = util.scand("dd-mmm-yyyy HH:MM",strEnd);
var diff = dateEnd.getTime() - dateStart.getTime();
var oneMin = 60 * 1000;
var mins = Math.floor (diff/oneMin);

event.value = mins;
}
else
event.value = "";

Likes

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
Explorer ,
Jul 07, 2022 Jul 07, 2022

Copy link to clipboard

Copied

nvm I lied

when I do this it gives me and answer in whole hours instead of lets say 5.2 hours

 

var oneHour = 60 * 60* 1000;
var hours = Math.floor (diff/oneHour);

event.value = hours;

 

Likes

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 ,
Jul 07, 2022 Jul 07, 2022

Copy link to clipboard

Copied

Remove the Math.floor() command...

Likes

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
Explorer ,
Jul 07, 2022 Jul 07, 2022

Copy link to clipboard

Copied

LATEST

Thanks 

Likes

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