Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

112
Visualizações
JS: Insert a value into input type date

I want to insert a current date into an input in form with javascript, but it is not working, here are my codes:

document.addEventListener("DOMContentLoaded", function() {
  document.getElementById("registrationday").value = getDay()

  function getDay() {
    var today = new Date();
    var dd = String(today.getDate()).padStart(2, '0');
    var mm = String(today.getMonth() + 1).padStart(2, '0');
    var yyyy = today.getFullYear();

    today =  "\"" + yyyy + '-' + mm + '-' + dd + "\"";
    return today
  }
})
<input type="date" id="registrationday" name="registrationday" disabled>

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Remove the quotes:

document.addEventListener("DOMContentLoaded", function() {
  document.getElementById("registrationday").value = getDay()

  function getDay() {
    var today = new Date();
    var dd = String(today.getDate()).padStart(2, '0');
    var mm = String(today.getMonth() + 1).padStart(2, '0');
    var yyyy = today.getFullYear();

    today =  yyyy + '-' + mm + '-' + dd;
    return today
  }
})
<input type="date" id="registrationday" name="registrationday" disabled>

However, a better solution is just to assign the current date to the input's valueAsDate property (suggested by @Always Helping):

document.addEventListener("DOMContentLoaded", function() {
  document.getElementById("registrationday").valueAsDate = getDay()

  function getDay() {
    return new Date()
  }
})
<input type="date" id="registrationday" name="registrationday" disabled>

about 4 years ago · Juan Pablo Isaza Relatório

0

var currentDate= new Date();

var day = ("0" + currentDate.getDate()).slice(-2);
var month = ("0" + (currentDate.getMonth() + 1)).slice(-2);

var today = currentDate.getFullYear()+"-"+(month)+"-"+(day) ;

document.getElementById("datepicker").value = today;
about 4 years ago · Juan Pablo Isaza Relatório

0

Solved with this JS:

document.addEventListener("DOMContentLoaded", function(){
var date = new Date ();
var day = datum.getDate(),
    month = datum.getMonth() + 1,
    year = datum.getFullYear();
var today = day + "-" + month + "-" + day ;

document.getElementById('registrationday').value = today;  
});

HTML:

<input type="date" id="registrationday" name="registrationday" disabled>
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda