Copy link to clipboard
Copied
// Define o número de dias a ser adicionado
var diasParaAdicionar = 35;
// Obtém o valor do campo "Date1" como string
var sDate1 = this.getField("Date1").valueAsString;
// Verifica se o campo inicial está vazio
if (sDate1 === "") {
event.value = ""; // Limpa o campo atual se o inicial estiver vazio
} else {
// Converte a string da data para um objeto Date do JavaScript,
// especificando o formato (neste exemplo, "mm/dd/yyyy")
var d1 = util.scand("mm/dd/yyyy", sDate1);
// Adiciona os dias à data. O método setDate() lida automaticamente
// com a virada de mês e ano, se necessário.
d1.setDate(d1.getDate() + diasParaAdicionar);
// Formata a nova data de volta para uma string no formato desejado
// e define o valor do campo atual
event.value = util.printd("mm/dd/yyyy", d1);
}Copy link to clipboard
Copied
Hi @lucidio_6830 ,
The name of the field for Calculation2 in your form is spelled like this : "Calculation 2" (with a blank space before the number 2), and not "Calculation2" (as expressed in your equations).
Field names in an equation must be spelled verbatim as they were created when you added the PDF objects to the form, otherwise just go ahead and adjust the field name in your equation to "Calculation 2" (instead of Calculation2).
The JavaScript console will keep throwing an error until this is fixed.
Copy link to clipboard
Copied
Check the field calculation order.
Copy link to clipboard
Copied
I don't know if my friend understood. I didn't understand the order of the calculations. In the section below:
// Define o número de dias a ser adicionado
var diasParaAdicionar = Calculo2;
I wanted it to work with Calculation2 replacing the number 35.
Copy link to clipboard
Copied
Use this:
var diasParaAdicionar = Number(this.getField("Calculo2").value);
You will still need to check the calculation order, though, to make sure that Calculo2 is calculated before this field. This is done outside of the code, though, under More - Set Fields Calculation Order in Prepare Form mode.
Copy link to clipboard
Copied
Good morning friend. I couldn't solve it. I did everything correctly, but the date I want isn't showing up, that is, x days from any given date.
// Defines the number of days to be added
var daysToAdd = Number(this.getField("Calculation2").value);
And I did the calculations in the correct order.
Lucidio
Copy link to clipboard
Copied
Why have you changed the name of the variable?
Copy link to clipboard
Copied
Share the file for further help.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
var diasParaAdicionar = Number(this.getField("Calculo2").value);
This field doesn't exists in the form.
Copy link to clipboard
Copied
Hi @lucidio_6830 ,
The name of the field for Calculation2 in your form is spelled like this : "Calculation 2" (with a blank space before the number 2), and not "Calculation2" (as expressed in your equations).
Field names in an equation must be spelled verbatim as they were created when you added the PDF objects to the form, otherwise just go ahead and adjust the field name in your equation to "Calculation 2" (instead of Calculation2).
The JavaScript console will keep throwing an error until this is fixed.
Copy link to clipboard
Copied
It worked, friend. I fixed it and it worked. Thank you.
Copy link to clipboard
Copied
I still need to clarify a doubt regarding the difference between two dates on this same calculator.
var date1 = this.getField("Date1").value;
var date1 = this.getField("Date2").value;
if (data1 && date1) {
// Converte as datas para objetos Date do JavaScript
var d2 = new Date(date2);
var d1 = new Date(data1);
// Calcula a diferença em milissegundos
var diff = d2.getTime() - d1.getTime();
// Converte milissegundos para dias (1000ms * 60s * 60m * 24h) e arredonda
var dias = Math.round(diff / (1000 * 60 * 60 * 24));
// Define o valor do campo atual (o campo calculado)
event.value = dias;
} else {
event.value = ""; // Limpa o campo se as datas não forem válidas
}
I used the script below, but it's not working. The difference would be today's date - the date of the exam.
Copy link to clipboard
Copied
You use date1 for both dates. Check the JavaScript console for errors.
Copy link to clipboard
Copied
Also, the Date object constructor is not doing what you think it does... Run this code in the JS Console to see what's happening:
new Date("02/12/25")
Copy link to clipboard
Copied
You can pull the number from the previous calculation field instead of hardcoding it. For example, if the previous calculation is in a field named calculo2, replace 35 with its value:
✅ Make sure calculo2 is calculated before this script runs, otherwise it will return 0 or NaN.
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more