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

Javascript - Subtract fields time

New Here ,
Aug 22, 2016 Aug 22, 2016

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

TOPICS
Acrobat SDK and JavaScript
2.2K
Translate
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 ,
Aug 22, 2016 Aug 22, 2016
Translate
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
New Here ,
Aug 23, 2016 Aug 23, 2016
LATEST

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

Thanks! I will read it.

Translate
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
LEGEND ,
Aug 22, 2016 Aug 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"?

Translate
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
LEGEND ,
Aug 22, 2016 Aug 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"?

Translate
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