I have the following code that adds the days entered in the input and shows them, but the date is shown as DD-M-YYYY, so when I save it in sql it sends me an error.
<input type="text" id="fechaImagen" onclick="calculoImagen();" >
<input type="number" id="frecuenciaDias" value="90" onclick="vaciarFechaSiguiente();" onchange="calculoImagen();" >
<input type="text" id="siguienteImagen">
<script type="text/javascript">
var f = new Date();
fecha = f.getDate() + "/" + (f.getMonth() +1) + "/" + f.getFullYear();
document.getElementById('fechaImagen').value = fecha;
function calculoImagen() {
tiempo = document.getElementById('frecuenciaDias').value;
addTime = tiempo * 86400; //Tiempo en segundos
f.setSeconds(addTime); //Añado el tiempo
fechaSiguienteImagen = f.getDate() + "/" + (f.getMonth() + 1) + "/" + f.getFullYear();
document.getElementById('siguienteImagen').value = fechaSiguienteImagen;
};
function vaciarFechaSiguiente() {
document.getElementById('siguienteImagen').reset();
}
</script>
How could I set the date to show it in this format DD-MM-YYYY?
I tell you that you can achieve it in the following way
Where you can notice the following:
So it gives this format "14-6-2018"
In that post I made an answer. Here I accommodate what you ask for.
To get the month the corresponding zeros on the left you can use the function
padStart(2,'0')
:You can also apply this for days that have only one digit.
Example:
Brother I hope it helps you
Using slice(-2)