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

184
Vistas
HTML input date field, how to set default value to today's date?

I'm slightly confused as to why my JS script isn't working, I have set it up to populate the date field to today's date, but the HTML date picker is still showing dd/mm/yyyy by default.

my HTML is:

<div class="col">
        <label for="date">Date</label>
        <input type="date" onload="getDate()" class="form-control" id="date" name="date">
</div>

my JS is:

function getDate(){
var today = new Date();

document.getElementById("date").value = today.getFullYear() + '-' + ('0' + (today.getMonth() + 1)).slice(-2) + '-' + ('0' + today.getDate()).slice(-2);

}

function getDate(){
var today = new Date();

document.getElementById("date").value = today.getFullYear() + '-' + ('0' + (today.getMonth() + 1)).slice(-2) + '-' + ('0' + today.getDate()).slice(-2);

}
 <div class="col">
           <label for="date">Date</label>
           <input type="date" onload="getDate()" class="form-control" id="date" name="date">
 </div>

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

0

For input elements, the onload attribute is only supported when <input type="image">

Tip: You use DOMContentLoaded for setting the default value

<script>
    window.addEventListener('DOMContentLoaded', (event) => {
        document.getElementById("date").valueAsDate = new Date();
    });
</script>

Reference: https://www.w3schools.com/tags/att_onload.asp

about 4 years ago · Juan Pablo Isaza Denunciar

0

I had some problems with that as well. I arrived at:

document.getElementById('mydate').value = new Date().toISOString().substring(0, 10);
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