Skip to main content
Participant
August 22, 2016
Question

Javascript - Subtract fields time

  • August 22, 2016
  • 3 replies
  • 2229 views

Hello,

I don't know to subtract two fields in Acrobat Forms using Javascript.

I have read a lot of information but all is using dates not time.

I have two fields: time-start and time-end and there are a third field with show the total.

Parameters of fields:

time-start (field): format time

time-end (field): format time

total-time(field): format time, time-end - time-start

The code is:

var start = this.getField("Start time").value;

var end = this.getField("End time").value;

var diff =  end - start

this.getField("Total time").value = diff

Is posible to do this calculation?

Thanks in advance

This topic has been closed for replies.

3 replies

Inspiring
August 22, 2016

What are some sample values and the results using your script?

If one uses values like "9:00 am" for the start and "12:30 pm" for the end time I get "NaN" or "Not a Number".

In order to perform simple arithmetic calculations one needs to use numeric values. These values are formatted like sign (+/-) followed by 1 more numbers in the range of 0-9, a decimal point and then more numeric digits. The ":" and "am" or "pm" are not numerics. Including any of those values will change the type of value from "number" to "string". There is no "-" operator for strings The "+" operator when used with a strings concatenates the values and does not add the values.

With some advanced programing using the RegExp object one can tease out the hours and minutes. With the hours and minutes one has a couple of options for computing the elapsed times.  This approach will work for times within the same day but fails if the times occur on different dates. In this case one needs to adjust for difference in the dates since each day represents a 24 hour difference that needs to be included. This is further complicated by having to deal with the time changes for DST, Day Light Savings Time, or Summer Time. This will add one hour in the fall sue the turn back of the clocks at 2:00 am.

Timesheets that use start and end times perform your calculation 7 times a week. Have you searched for "Timesheets"?

Inspiring
August 22, 2016

2What are some sample values and the results using your script?

If one uses values like "9:00 am" for the start and "12:30 pm" for the end time I get "NaN" or "Not a Number".

In order to perform simple arithmetic calculations one needs to use numeric values. These values are formatted like sign (+/-) followed by 1 more numbers in the range of 0-9, a decimal point and then more numeric digits. The ":" and "am" or "pm" are not numerics. Including any of those values will change the type of value from "number" to "string". There is no "-" operator for strings The "+" operator when used with a strings concatenates the values and does not add the values.

With some advanced programing using the RegExp object one can tease out the hours and minutes. With the hours and minutes one has a couple of options for computing the elapsed times.  This approach will work for times within the same day but fails if the times occur on different dates. In this case one needs to adjust for difference in the dates since each day represents a 24 hour difference that needs to be included. This is further complicated by having to deal with the time changes for DST, Day Light Savings Time, or Summer Time. This will add one hour in the fall sue the turn back of the clocks at 2:00 am.

Timesheets that use start and end times perform your calculation 7 times a week. Have you searched for "Timesheets"?

Participant
August 23, 2016

I think that the operations with date and time it will be more easy...

Thanks! I will read it.