Chronoforms - Autocalculate AGE with javascript

Kronosites 02 Feb, 2016
Hello!

I need to obtain the age and show in my form automatically when an user inserts 2 dates into the form.
(This is a JAVASCRIPT question, but I think it could be usefull for all the people that are using Chronoforms, and I'm sure someone have experience with javascript and will help me).

I have a JS code that calculates the age from the birth date to "today date", and uses dates with YYYY-MM-DD format. I need use dates in DD-MM-YYYY format and calculate the age between "birth date" (p101ifechanac) and "regiter date"(p101dfechaingreso).

>> Could you help me please?

Example (my form):
Input Birth date: p101ifechanac
Input Register Date: p101dfechaingreso
Input (to show the age): p101iedad

We have to add "FunctionCall" inside the form, through the menu Design->Events, just like this:
on CHANGE VALUE of p101fechanac FUNCTION calcularEdad()

And here you are the javascript code:
function isValidDate(day,month,year)
{
    var dteDate;
    month=month-1;
    dteDate=new Date(year,month,day);
    return ((day==dteDate.getDate()) && (month==dteDate.getMonth()) && (year==dteDate.getFullYear()));
}
 
function validate_fecha(fecha)
{
    var patron=new RegExp("^(19|20)+([0-9]{2})([-])([0-9]{1,2})([-])([0-9]{1,2})$");
 
    if(fecha.search(patron)==0)
    {
        var values=fecha.split("-");
        if(isValidDate(values[2],values[1],values[0]))
        {
            return true;
        }
    }
    return false;
}
 
function calcularEdad()
{
    var fecha=document.getElementById("p101ifechanac").value;
    if(validate_fecha(fecha)==true)
    {
        // Si la fecha es correcta, calculamos la edad
        var values=fecha.split("-");
        var dia = values[2];
        var mes = values[1];
        var ano = values[0];
 
        // cogemos los valores actuales
        var fecha_hoy = new Date();
        var ahora_ano = fecha_hoy.getYear();
        var ahora_mes = fecha_hoy.getMonth()+1;
        var ahora_dia = fecha_hoy.getDate();
 
        // realizamos el calculo
        var edad = (ahora_ano + 1900) - ano;
        if ( ahora_mes < mes )
        {
            edad--;
        }
        if ((mes == ahora_mes) && (ahora_dia < dia))
        {
            edad--;
        }
        if (edad > 1900)
        {
            edad -= 1900;
        }
 
        // calculamos los meses
        var meses=0;
        if(ahora_mes>mes)
            meses=ahora_mes-mes;
        if(ahora_mes<mes)
            meses=12-(mes-ahora_mes);
        if(ahora_mes==mes && dia>ahora_dia)
            meses=11;
 
        // calculamos los dias
        var dias=0;
        if(ahora_dia>dia)
            dias=ahora_dia-dia;
        if(ahora_dia<dia)
        {
            ultimoDiaMes=new Date(ahora_ano, ahora_mes, 0);
            dias=ultimoDiaMes.getDate()-(dia-ahora_dia);
        }
 
        document.getElementById("p101iedad").value=""+edad+"";
    }else{
        document.getElementById("p101iedad").value="La fecha "+fecha+" es incorrecta";
    }
}
GreyHead 02 Feb, 2016
HI Kronosites,

Please check this post and see if that does what you need.

Bob
Kronosites 04 Feb, 2016
1 Likes
Hello GreyHead,

The post you left me dont solve the problem, cause use YYYT-MM-DD and uses only 1 variable.
[...]
BUT I find the solution, you can leave as "Manual" for other users:

Title: How to calculate the age between 2 dates in Chronoforms v5 (with javascript)
1- Create your form. You will need, at least, 3 boxes: 1 for "birthdate", 1 for "nowdate", and other to show the "result".
[list]Note: ID must be equal to "birthdate", "nowdate" and "result".
Note2: The dates must be set ad DD-MM-YYYY[/list]
2- Add 2 events on Design-Events menú (Chronoforms), like this:
[list]ON Change value OF birthdate FUNCTION calculateage()
ON Change value OF nowdate FUNCTION calculateage()[/list]
3- Go to SETUP and add a "Load Javascript" inside OnLoad event, before the render form.
4- Add the next code insite the "Load Javascript".

function calculateage()
{
    var fecha=document.getElementById("birthdate").value;
   var justnow=document.getElementById("nowdate").value;
 
        var values=fecha.split("-");
        var dia = values[0];
        var mes = values[1];
        var ano = values[2];
 
        var rightnow=justnow.split("-");
        var ahora_dia = rightnow[0];
        var ahora_mes = rightnow[1];
        var ahora_ano = rightnow[2];
 
        // realizamos el calculo
        var edad = (ahora_ano) - ano;
        if ( ahora_mes < mes )
        {
            edad--;
        }
        if ((mes == ahora_mes) && (ahora_dia < dia))
        {
            edad--;
        }
    document.getElementById("result").value=""+edad+"";
}


¡Thats all!
This topic is locked and no more replies can be posted.