Problems with Date () javascript methods

0

I am trying to learn to validate fields with js, but as much as I look for information both on the Internet and around here I do not see anything that helps me solve it. I need to know that the difference of dates is > 18 years. In the html code, I have an event for it.
Can someone give me some clue? Thanks

function validarFecha(){
     
var ahora = new Date();
var fIntroducida = new Date(introducida.value);
    
var introducida = document.getElementById('inputBirthDate').value;

var mesActual = ahora.getMonth()+1; //+1 empieza en 0
var diaActual = ahora.getDate();
var anoActual = ahora.getFullYear();   

var mesFor = fIntroducida.getMonth()+1; //+1 empieza en 0
var diaFor = fIntroducida.getDate();
var anoFor = fIntroducida.getFullYear();   
    
if (anoActual - anoFor >=18){
    if(mesActual > mesFor){
        return 1;
        
        if(diaActual >= diaFor){
        return 1;    
        }
   
    }
    
} else {
    return 0;
}

}
    
asked by Mrs Prerobot 24.11.2018 в 21:44
source

1 answer

0

I do not see a priori anything wrong with your function, except for some confusion maybe. The variable "introduced":

var introducida = document.getElementById('inputBirthDate').value;

you have declared it after the variable "fIntroducida", which takes (needs) the value of the other. However, keep in mind that in order to obtain a valid date type object, unless you give it a format of type DD-MM-YYYY or another, the appropriate format must be YYYY-MM-DD (look at the date that I've given as an example).

And you only have one more case that you have not covered with else's. For the rest, everything ok.

Take a look at the example.

I hope I helped you. Greetings.

validarFecha();
function validarFecha(){
     
var ahora = new Date();
var fIntroducida = new Date('2000-12-24');
    
//var introducida = document.getElementById('inputBirthDate').value;

var mesActual = ahora.getMonth()+1;
var diaActual = ahora.getDate();
var anoActual = ahora.getFullYear();

var mesFor = fIntroducida.getMonth()+1;
var diaFor = fIntroducida.getDate();
var anoFor = fIntroducida.getFullYear();

if (anoActual - anoFor >=18){

    if(mesActual >= mesFor){

        if(diaActual >= diaFor){
    		document.write('mayor'); 
        }
        else{
        	document.write('menor'); 
        }
   
    }
    else{
    	document.write('menor');
    }
    
} else {
	document.write('menor'); 
}

}

Or with several examples today (11/24/2018):

var fechas_ejemplo = ['2000-11-23','2000-11-24','2000-11-25'];
for(var i=0;i<fechas_ejemplo.length;i++){
	document.write('fecha'+(i+1)+': '+fechas_ejemplo[i]+' Resultado: ');
	validarFecha(fechas_ejemplo[i]);
	document.write(' | ');
}


function validarFecha(fecha){
     
var ahora = new Date();
var fIntroducida = new Date(fecha);

var mesActual = ahora.getMonth()+1;
var diaActual = ahora.getDate();
var anoActual = ahora.getFullYear();

var mesFor = fIntroducida.getMonth()+1;
var diaFor = fIntroducida.getDate();
var anoFor = fIntroducida.getFullYear();

if (anoActual - anoFor >=18){

    if(mesActual >= mesFor){

        if(diaActual >= diaFor){
    		document.write('mayor'); 
        }
        else{
        	document.write('menor'); 
        }
   
    }
    else{
    	document.write('menor');
    }
    
} else {
	document.write('menor'); 
}

}

To put it in the appropriate format, if for example your date has the format dd / mm / yyyy:

var fecha_inicial = "24/11/2000";

var fecha_array = fecha_inicial.split("/"); //la conviertes en un array

var dateObject = new Date(fecha_array[2], fecha_array[1] - 1, fecha_array[0]); //la reordenas y la conviertes en un objeto date válido
    
answered by 24.11.2018 в 22:20