Skip to main content
Inspiring
February 27, 2025
Answered

Idade

  • February 27, 2025
  • 2 replies
  • 1221 views

Boa tarde,

Tenho um formulário com um campo "Data de Nascimento" (nome do campo "data_nascimento") e outro campo para calcular a idade com relação a data atual (nome do campo "idade")

no campo idade, na guia "Calcular" - script de calculo personalizado, coloquei o script abaixo:

 

var docString = this.getField("dd").valueAsString + "/" + this.getField("mm").valueAsString + "/" + this.getField("Year").valueAsString; var dob = util.scand("dd/mm/yyyy", this.getField("data_nascimento").valueAsString); if (dob != null) { var today = util.scand("mm/dd/yyyy", docString); var age = today.getFullYear() - dob.getFullYear(); var m = today.getMonth() - dob.getMonth();

if (m < 0 || (m === 0 && today.getDate() < dob.getDate()))

{ age--; } event.value = age; }

else

{ event.value = ""; } if(event.value == 0) event.value = "";

 

Código entrado aqui no forum e que dizem que esta a funcionar, mas não consigo fazer, alguém pode me ajudar?

Correct answer PDF Automation Station

Your script won't work because you don't have the fields that are called in the script.  The following script will work as a custom calculation in the age field for a date_birth formatted as mm/dd/yyyy

function Scand(cFormat, cValue) 
{
var oDate = util.scand(cFormat, cValue);
return new Date(oDate.getFullYear(), oDate.getMonth(), oDate.getDate(), oDate.getHours(), oDate.getMinutes(), oDate.getSeconds(), 0);
} 

event.value = "";
var dob=this.getField("date_birth").value;


if(dob != "") 
{
var oDob = Scand("mm/dd/yyyy" + " hh:mm:ss", dob + " 00:00:00") ;
var oToday = Scand("mm/dd/yyyy" + " hh:mm:ss", util.printd("mm/dd/yyyy", new Date()) + "00:00:00");
var age = oToday.getFullYear() - oDob.getFullYear();
if(util.printd("mmdd", oToday) < util.printd("mmdd", oDob) ) 
{age--;}
event.value = age;
}

2 replies

PDF Automation Station
Community Expert
Community Expert
February 27, 2025

Your script won't work because you don't have the fields that are called in the script.  The following script will work as a custom calculation in the age field for a date_birth formatted as mm/dd/yyyy

function Scand(cFormat, cValue) 
{
var oDate = util.scand(cFormat, cValue);
return new Date(oDate.getFullYear(), oDate.getMonth(), oDate.getDate(), oDate.getHours(), oDate.getMinutes(), oDate.getSeconds(), 0);
} 

event.value = "";
var dob=this.getField("date_birth").value;


if(dob != "") 
{
var oDob = Scand("mm/dd/yyyy" + " hh:mm:ss", dob + " 00:00:00") ;
var oToday = Scand("mm/dd/yyyy" + " hh:mm:ss", util.printd("mm/dd/yyyy", new Date()) + "00:00:00");
var age = oToday.getFullYear() - oDob.getFullYear();
if(util.printd("mmdd", oToday) < util.printd("mmdd", oDob) ) 
{age--;}
event.value = age;
}
Inspiring
February 28, 2025

Infelizmente não resultou  nenhuma das alternativas, só se eu estiver a colocar no local errado.

try67
Community Expert
Community Expert
February 28, 2025

Check the JS Console for errors. If you still can't figure it out, share your file.

Inspiring
February 27, 2025

pedi para o chatgpt criar um script, ele pediu para na opção "Ação" em "Execultar javascritp" colocar o script abaixo.

Mas também não resultou,  o que estou a fazer errado?

// Obtém o valor da data de nascimento
var dataNascimento = this.getField("DataNascimento").value;

if (dataNascimento) {
    // Divida a data em dia, mês e ano
    var partes = dataNascimento.split("/");

    // Verifica se a data está no formato correto (dd/mm/aaaa)
    if (partes.length === 3) {
        var diaNascimento = parseInt(partes[0], 10);
        var mesNascimento = parseInt(partes[1], 10) - 1; // Meses começam do 0
        var anoNascimento = parseInt(partes[2], 10);

        // Cria a data de nascimento
        var dataNasc = new Date(anoNascimento, mesNascimento, diaNascimento);

        // Cria a data atual
        var dataAtual = new Date();

        // Calcula a idade
        var idade = dataAtual.getFullYear() - dataNasc.getFullYear();
        var mesAtual = dataAtual.getMonth();
        var diaAtual = dataAtual.getDate();

        // Ajusta a idade caso o aniversário ainda não tenha ocorrido este ano
        if (mesAtual < mesNascimento || (mesAtual === mesNascimento && diaAtual < diaNascimento)) {
            idade--;
        }

        // Exibe a idade no campo "Idade"
        this.getField("Idade").value = idade;
    } else {
        // Caso a data não esteja no formato correto
        this.getField("Idade").value = "Formato inválido";
    }
} else {
    // Caso o campo de data de nascimento esteja vazio
    this.getField("Idade").value = "";
}
try67
Community Expert
Community Expert
February 27, 2025

If this is a calculation script changes all instances of:

this.getField("Idade").value =...

To:

event.value =...

 

After doing that, if it still doesn't work, check the JS Console for error messages.