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

Idade

Explorer ,
Feb 27, 2025 Feb 27, 2025

Copy link to clipboard

Copied

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

Views

105
Translate

Report

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

Copy link to clipboard

Copied

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

Votes

Translate

Report

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
Explorer ,
Feb 27, 2025 Feb 27, 2025

Copy link to clipboard

Copied

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 = "";
}

Votes

Translate

Report

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

Copy link to clipboard

Copied

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.

Votes

Translate

Report

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

Copy link to clipboard

Copied

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;
}

Votes

Translate

Report

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

Copy link to clipboard

Copied

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

Votes

Translate

Report

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

Copy link to clipboard

Copied

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

Votes

Translate

Report

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

Copy link to clipboard

Copied

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?

Votes

Translate

Report

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

Copy link to clipboard

Copied

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

Votes

Translate

Report

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

Copy link to clipboard

Copied

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

Votes

Translate

Report

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

Copy link to clipboard

Copied

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.ErroErroexpand imageCaptura de ecrã 2025-03-03, às 18.07.26.pngexpand imageCaptura de ecrã 2025-03-03, às 18.07.33.pngexpand imageJá não existe mais o script "Calcular_idade"Já não existe mais o script "Calcular_idade"expand image

Votes

Translate

Report

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

Copy link to clipboard

Copied

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.

Votes

Translate

Report

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

Copy link to clipboard

Copied

LATEST

Muito obrigado, funcionou

Votes

Translate

Report

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