Skip to main content
WorldWarrior
Participating Frequently
June 22, 2019
Question

Get End Time / Using Start Time and Hours Worked

  • June 22, 2019
  • 2 replies
  • 952 views

Hello World,

I'm still a Newbie to Acrobat and JavaScript.

I've been combing through the forums, Pubs and Manuals to create this code and cannot seem to get it to give me my answer.

I mean it doesn't display the result in my "StopTime" Text-field (Time - h:MM tt) and because I cannot figure out how to view my results line by line.

I don't know if it's because of prior code.

Could someone please advise what I'm doing wrong.

Here is the code I've created;

// get the start time and added time

var sStart = this.getField('StartTime').value;

var sWork = this.getField('Work').value;

var sTravel = this.getField('Travel').value;

//Convert Time to seconds

var vStart = Time2Num(sStart.FormattedValue, "h:MM tt");

var vWork = (sWork * 3600000);

var vTravel = (sTravel * 3600000);

If(HasValue('StartTime'))

{

    If(HasValue('Work') & HasValue('Travel'))

    {

          // Add Time

          var vTotal = vStart + vWork + vTravel;

    }

    ElseIf(HasValue('Work'))

    {

          // Add Time

          var vTotal = vStart + vWork;

    }

    Else

    {

        // Add Time

        var vTotal = vStart + vTravel;

    }

}

// Convert to time

var vStop = Num2Time(vTotal, "HH:MM");

// Display Time

event.value = ''

'StopTime'.value = util.printd("h:MM tt", vStop);

Any help would be greatly appreciated.

This topic has been closed for replies.

2 replies

WorldWarrior
Participating Frequently
June 26, 2019

Found the answer,

Ended up being simpler then I thought, I didn't need to destruct the time strings.

Add the results and convert back using Functions.

Anyone that needs to add worked hours to a start time here is what I found and did.

Might be a little amateur, but I am new to Acrobat JavaScript (I'll add some ForLoops in later.

And it's good for 24hr logging.

(function ()

    {

        // Get field values as numbers and calculate sum

        var vServiceRow1 = +getField("ServiceRow1").value;

        var vServiceRow2 = +getField("ServiceRow2").value;

        var vServiceRow3 = +getField("ServiceRow3").value;

        var vServiceRow4 = +getField("ServiceRow4").value;

        var vServiceRowSum = vServiceRow1 + vServiceRow2 + vServiceRow3 + vServiceRow4;

        var vWarrantyRow1 = +getField("WarrantyRow1").value;

        var vWarrantyRow2 = +getField("WarrantyRow2").value;

        var vWarrantyRow3 = +getField("WarrantyRow3").value;

        var vWarrantyRow4 = +getField("WarrantyRow4").value;

        var vWarrantyRowSum = vWarrantyRow1 + vWarrantyRow2 + vWarrantyRow3 + vWarrantyRow4;

        var vIdleRow1 = +getField("IdleRow1").value;

        var vIdleRow2 = +getField("IdleRow2").value;

        var vIdleRow3 = +getField("IdleRow3").value;

        var vIdleRow4 = +getField("IdleRow4").value;

        var vIdleRowSum = vIdleRow1 + vIdleRow2 + vIdleRow3 + vIdleRow4;

        var vBreak = +getField("Break").value;

        event.value = vServiceRowSum + vWarrantyRowSum + vIdleRowSum + vBreak;

        if (event.value == 0)

            {

                event.value = "";

            }

        else

            {

                event.value;

            }

    }

)();

event.value = ""; 

var vStartTime = this.getField("StartTime").valueAsString; 

if (vStartTime!="")

{

    var d = util.scand("mm/dd/yyyy HH:MM", "01/01/2017 " + vStartTime);

    var v = this.getField("Work").valueAsString;

    if (v!="")

    {

        var addTimeHours = Number(v);

        var msPerHour = 3600000;

        d.setTime(d.getTime() + (addTimeHours * msPerHour));

        event.value = util.printd("HH:MM", d);

    } 

}try67

try67
Community Expert
Community Expert
June 22, 2019

If you copy and paste code from other scripts you have to make sure you copy all of the relevant parts. All of the functions you're using in your code (HasValue, Time2Num, Num2Time, etc.) are not defined in your code, so it can't work.

WorldWarrior
Participating Frequently
June 22, 2019

try67,

Thanks for your reply, I typed them in straight up.

As I said new to Acrobat Javascript, I assumed these were typical commands as they were in the manual.

So do I need to import something into my form for these functions to work like a macro of some kind.

Thanks for the quick reply,

try67
Community Expert
Community Expert
June 22, 2019

You need to find the definitions of those functions and include them in your code, yes.