Copy link to clipboard
Copied
My Javascript code for calculating a value from a specific date difference the debugger complains with no understandable reasons.
E.g. "GetFullYear is not a function" although These are normal Javascript functions.
This is the code:
var PastDate = this.getField("Datum1").value;
var jetzt = new Date();
PastDate = PastDate.split(".");
var DateDiff = jetzt.GetFullYear()-PastDate[2] + 12*(jetzt.GetMonth() - PastDate[1]);
this.getField("€_2").value = DateDiff / 12 * this.getField("Prozent1").value / 100 * this.getField("€").value;
Could anybody inform me please what is wrong at the script and where I can find a suiting documentation to easily build such scripts?
Cause the Adobe Javasript API Acrobat DC SDK Documentation does not really help.
Thank you very much.
Copy link to clipboard
Copied
GetFullYear is not the same than getFullYear.
JavaScript is case sensitive.
You will have the same issue with GetMonth, etc.
Copy link to clipboard
Copied
Oh yes, thank you VERY much.
I was not aware of that.
The debugger also complained the split function. So I assumed that the problem is much bigger.
But now this works fine as well.
Thank you again!!
Copy link to clipboard
Copied
Just a tip: If you're going to be using a field value as a string (e.g., by using string methods with it), you should get the field value using the valueAsString property, as opposed to the value property. It's just a good habit to get into.
Copy link to clipboard
Copied
Another tip to all script writers out there: If you want to use the value of a field in a calculation, always convert it explicitly into a number, by using the Number object constructor. For example:
Number(this.getField("FieldName").valueAsString)
This is especially important if you add values, as the "+" operator has a double function: When used for numbers it adds them up, as expected, but when used for strings it concatenates them, so if either one of the values added is actually a string, the result will be a concatenated string. For example:
1 + 1 = 2
1 + "1" = "11"
"1" + 1 = "11"
"1" + "1" = "11"
Copy link to clipboard
Copied
Thank you again!
Actually I had this kind of problem at last but your tipp didn't help.
This was my code:
var startDate = Number(this.getField("Datum1").valueAsString); // 01.08.2017
var endDate = Number(this.getField("Datum1b").valueAsString); // 08.04.2018
startDate = startDate.split(".");
endDate = endDate.split(".");
var DateDiff = 12*(endDate[2]-startDate[2]) + endDate[1]-startDate[1]; // =1096
In fact you had to put the last substraction into brackets to get the right result 8!!
var startDate = this.getField("Datum1").valueAsString; // 01.08.2017
var endDate = this.getField("Datum1b").valueAsString; // 08.04.2018
startDate = startDate.split(".");
endDate = endDate.split(".");
var DateDiff = 12*(endDate[2]-startDate[2]) + (endDate[1]-startDate[1]); // =8
Copy link to clipboard
Copied
You should only convert it to a number if you're going to use it as a number. In your script, you're using the field value as a string, so you shouldn't use the Number function.
Copy link to clipboard
Copied
Date strings (like "01.08.2017") are not numbers, so you should not use the Number constructor on them...
Copy link to clipboard
Copied
Yes, that's clear to me.
But that still doesn't explain why I need that last bracket. mathematically it is NOT necessary at all!!
I needed some time to find out that this is the script's problem...
The type conversion does not seem to work properly here. The '+* seems to work as a concatenation. Means that the splitted arrays have not been handled as numbers but still as strings!...
If you write:
var DateDiff = 12*(endDate[2]-startDate[2]) + Number(endDate[1])-Number(startDate[1]);
or even this...
var DateDiff = 12*(endDate[2]-startDate[2]) + Number(endDate[1])-startDate[1];
...it works fine also without those brackets...
So this is somehow logical.
That the brackets work instead as well is strange, isn't it?...
Copy link to clipboard
Copied
Yes, the split method returns an array of strings, ​not numbers or anything else.
Copy link to clipboard
Copied
But then again: Why do the brackets help?...
Copy link to clipboard
Copied
I'm not sure which brackets you're referring to now...
Copy link to clipboard
Copied
I ment the brackets at ...(endDate[1]-startDate[1]);
Please check some messages before. I mentioned that already at 01.08.2018 12:15. 😉
Copy link to clipboard
Copied
The reason is that the minus operator converts the strings to numbers first, while the plus operator doesn't.
So if you try to compute this:
12*("08"-"02")+"05"-"04"
It will not work correctly (the result will be "7201"), because the moment you use the "+" operator on a string, the result will be a string as well, but if you use it like this:
12*("08"-"02")+("05"-"04")
Then it will work correctly (the result will be 73), because you're using the "+" operator only on numbers.
Bottom line, you should always convert your strings into numbers explicitly.