Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

152
Vistas
Problem when compare 2 dates in javascript

i have 1 problem, i wanna compare two dates between date born and today, the condition is date born must be less than today date, it worked at first, but if i pick another date from datepicker which is less than today, its still pop up alert. And it not ok. Anyone know why this this happened?

    function submitForm() {
        
        var dateborn = document.getElementById('dateborn').value;   
        var today = document.getElementById('today').value;
        
        if(dateborn != '')
        {           
            if(dateborn > today)
            {
            swal("Oops", "Date born must less than date today", "error");       
            return false;
            }
        }

            
    }
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

If I were you I can use the timestamp to compare 2 dates.

function submitForm() {
        
        var dateborn = document.getElementById('dateborn').value;   
        var today = document.getElementById('today').value;
        const time1 = new Date(dateborn).getTime();
        const time2 = new Date(today).getTime(); // or new Date().getTime();
        if(dateborn != '')
        {           
            if(time1 > time2)
            {
            swal("Oops", "Date born must less than date today", "error");       
            return false;
            }
        }
about 4 years ago · Juan Pablo Isaza Denunciar

0

Try this

function submitForm() {
    
    var dateborn = document.getElementById('dateborn').value;   
    var today = document.getElementById('today').value;
    
    if(dateborn != '')
    {           
        if(Date(dateborn).getTime() > Date(today).getTime())
        {
        swal("Oops", "Date born must less than date today", "error");       
        return false;
        }
    }

        
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

As for your question why this happened. i think your date picker does not give the date in ISO format. if your value is formatted like: YYYY-MM-DD (from biggest to smallest) you can compare it as a string. You can see this from this fiddle i made: JSfiddle

Does your date picker look like this:

<input type="date" id="d2" onChange="change(this)"/>
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda