1

Tengo mi input datetime-local, como puedo inicializar con javascript con la fecha y hora actual de mi maquina?

<input required type="datetime-local" placeholder="Fecha y Hora" class="form-control" id="fecha_hora">

Intente esto pero no aparece nada.

var hoy = new Date()
var fecha = hoy.getDate() + '-' + (hoy.getMonth() + 1) + '-' + hoy.getFullYear();
var hora = hoy.getHours() + '-' + hoy.getMinutes() + '-' + hoy.getSeconds();

var fechar_hora = fecha + hora;
document.getElementById('fecha_hora').val = fechar_hora;

1 respuesta 1

0

Intenta asignar el valor en el siguiente formato "2021-10-01T08:00"

var hoy = new Date()
var fecha = hoy.getFullYear() + '-' + ('0' + (hoy.getMonth() + 1)).slice(-2) + '-' + ('0' + hoy.getDate()).slice(-2) ;
var hora = ('0' + hoy.getHours()).slice(-2) + ':' + ('0' + hoy.getMinutes()).slice(-2);

var fecha_hora = fecha +'T'+ hora;
document.getElementById('fecha_hora').value = fecha_hora;
('0' + hoy.getDate()).slice(-2) //AÑADE CERO A LA IZQUIERDA SI EL VALOR SOLAMENTE TIENE UNA CIFRA
0

Tu Respuesta

Al pulsar en “Publica tu respuesta”, muestras tu consentimiento a nuestros términos de servicio, política de privacidad y política de cookies

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.