Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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,
Copy link to clipboard
Copied
You need to find the definitions of those functions and include them in your code, yes.
Copy link to clipboard
Copied
I tracked the source of the Functions, they are Adobe but a different format of Adobe Javascript.
Why can't they make one common set of source commands for all their line of products.
Looks like I'm going to have to write new ones for HasValue, Time2Num, Num2Time in Adobe Acrobat Javascript.
If you happen to find someone else's version adapted to Acrobat Javascript, please let me know.
Hope this Newbie is up to it, thanks for your direction.
Copy link to clipboard
Copied
I think all of this code comes from an LCD document, which is not the same as a regular PDF file, so you can't use it.
What is it exactly that you're trying to calculate?
Copy link to clipboard
Copied
I need to take the accumulated hours worked / traveled and add them to the start time of the service team member.
The resulting value being the finish time.
Adding a little error proofing of the daily log sheets.
Everything I found revolves around the date not the time.
Creation of a Function is a good idea for future edits and/or forms.
If you have a good starting place I'm all ears.
Thank You,
Copy link to clipboard
Copied
These tutorials are a good starting point:
https://acrobatusers.com/tutorials/working-with-date-and-time-in-acrobat-javascript
https://acrobatusers.com/tutorials/working-with-date-and-time-in-acrobat-javascript-part-2
https://acrobatusers.com/tutorials/working-date-and-time-acrobat-javascript-part-3-3
Also, if you're interested, I've developed a (paid-for) tool that allows you to set up such calculations very easily:
Custom-made Adobe Scripts: Acrobat -- Calculate Time Differences in a Worksheet
Copy link to clipboard
Copied
Thanks,
Seen all of these and they are all time difference between, known times.
I'm trying to create the second time.
I'll use them as a base to develop the reverse of Thom Parker work.
Read quite a bit of his stuff looking for what I need.
Thank you for the insight.
Copy link to clipboard
Copied
I could still develop it for you, if you wish. Contact me privately to discuss it further, please.
Copy link to clipboard
Copied
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​