Show current date in input type date with JavaScript

2

I need to show the current date in a date type input instead of "dd / mm / yyyy".

    
asked by Venturo Lévano Adrián 24.05.2018 в 21:14
source

2 answers

4

To do so, you should use JavaScript.

You can use the object Date , I recommend you read a bit about it to give it a more specific use

n =  new Date();
//Año
y = n.getFullYear();
//Mes
m = n.getMonth() + 1;
//Día
d = n.getDate();

//Lo ordenas a gusto.
document.getElementById("date").innerHTML = d + "/" + m + "/" + y;
<h1 id="date"></h1>
    
answered by 24.05.2018 в 21:21
3

You could try something like the following:

window.onload = function(){
  var fecha = new Date(); //Fecha actual
  var mes = fecha.getMonth()+1; //obteniendo mes
  var dia = fecha.getDate(); //obteniendo dia
  var ano = fecha.getFullYear(); //obteniendo año
  if(dia<10)
    dia='0'+dia; //agrega cero si el menor de 10
  if(mes<10)
    mes='0'+mes //agrega cero si el menor de 10
  document.getElementById('fechaActual').value=ano+"-"+mes+"-"+dia;
}
<input type="date" id="fechaActual" value="" >
    
answered by 24.05.2018 в 21:47