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

Idade

Participant ,
Feb 27, 2025 Feb 27, 2025

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?

TOPICS
How to , JavaScript , PDF
277
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
1 ACCEPTED SOLUTION
Community Expert ,
Feb 27, 2025 Feb 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;
}

View solution in original post

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
Participant ,
Feb 27, 2025 Feb 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 = "";
}
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 ,
Feb 27, 2025 Feb 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.

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 ,
Feb 27, 2025 Feb 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;
}
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
Participant ,
Feb 28, 2025 Feb 28, 2025

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

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 ,
Feb 28, 2025 Feb 28, 2025

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

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 ,
Feb 28, 2025 Feb 28, 2025

Did you put it as a custom calculation script in the age field?  Is your Date of Birth field named date_birth (case sensitive) and is it formatted as mm/dd/yyyy?

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
Participant ,
Mar 03, 2025 Mar 03, 2025

sim, esta tudo conforme você mencionou, mas não funciona, existe algum email que possa lhe enviar o arquivo?

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 ,
Mar 03, 2025 Mar 03, 2025

You can upload it right here by replying and clicking paperclip icon.

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
Participant ,
Mar 03, 2025 Mar 03, 2025

Aqui esta o arquivo, para mim esta a dar um erro em um script que já apaguei, já reparei o arquivo mas o erro continua.ErroErroCaptura de ecrã 2025-03-03, às 18.07.26.pngCaptura de ecrã 2025-03-03, às 18.07.33.pngJá não existe mais o script "Calcular_idade"Já não existe mais o script "Calcular_idade"

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 ,
Mar 03, 2025 Mar 03, 2025

You have two errors.  The field name is "datanascimento_af_date" not "datanascimento".  Change the field name in the script.  Also, you date field is formatted as dd/mm/yyyy.  Change all instances of mm/dd/yyyy to dd/mm/yyyy in the script and all instances of mmdd to ddmm.

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
Participant ,
Mar 03, 2025 Mar 03, 2025
LATEST

Muito obrigado, funcionou

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