Skip to main content
Participant
November 25, 2024
Question

If/max and ceiling statements in PDF Forms

  • November 25, 2024
  • 2 replies
  • 210 views

Hello,

 

I am trying to take a formula I use in Excel and apply it to the PDF invoice version for people to enter manually then they want. 

I have a PDF with forms and the fields I need to calculate are two time fields (HH:MM) and I want them to be subracting so that I can have the duration, but I need that duration in a number format and to 1 =  1 hour, and after that to go up in increments of 0.5. 

In Excel I use the formula:

=IF(C1<=TIME(1,0,0),1,CEILING(C1*24,0.5)) which can also be simplified (I believe, haven't tried) with

=MAX(1,CEILING(C1*24,0.5))

 

can I do the same in PDF Forms so that when a start and end time are entered into two seperate fields, I would end up with an integer in a seperate field?

 

Example:

Start Time Field = 12:30

End Time Field = 14:45

Calculation Field = 2,5

 

Thank you very much for your help!

This topic has been closed for replies.

2 replies

Nesa Nurani
Community Expert
Community Expert
November 25, 2024

Try this as custom calculation script of 'calculate' field (change "Start Time" and "End Time" to your actual field names):

var startTime = this.getField("Start Time").valueAsString;
var endTime = this.getField("End Time").valueAsString;

if (startTime && endTime) {
 var startParts = startTime.split(":");
 var endParts = endTime.split(":");
 var startMinutes = parseInt(startParts[0]) * 60 + parseInt(startParts[1]);
 var endMinutes = parseInt(endParts[0]) * 60 + parseInt(endParts[1]);
 var diffMinutes = endMinutes - startMinutes;
 var decimalHours = diffMinutes / 60;

 if (decimalHours <= 1) {
  event.value = 1;} 
 else {
  event.value = Math.ceil(decimalHours * 2) / 2;}} 
else {
 event.value = "";}