Javascript dates

Question:

Hello everyone, I had the following question, can it be done that when an input type="date " has passed more than 65 years of difference between today's date and the one that users put, an input type="radio" be deactivated input type="radio" ?

That is, first I need to get the value of the "date" field, and then subtract the date entered from today's date, and if it is greater than 65 years apart, a radio type field is deactivated, would that be possible?

Answer:

There you have the HTML and Javascript code. I think it's clear but if you have questions ask ^^

Instead of a button, you can make an OnChange event of type="date"

The calculation that appears in the IF is because the result is given in milliseconds and I pass it to years.

 function bloquearRadio(){ var fechaInput = new Date(document.getElementById("fecha").value); var dateNow = Date.now(); if((dateNow - fechaInput)/(1000 * 3600 * 24*365) > 65 ){ //alert("+65"); document.getElementById("radio").disabled = true; }else{ document.getElementById("radio").disabled = false; } }
 <form > Fecha: <input id="fecha" type="date" > <input onclick=bloquearRadio() type="button" value="Validar Fecha"> <input id="radio" type="radio" >No activo para más de 65 años </input> </form>
Scroll to Top