String for date and day subtraction javascript

0

I need to convert a date that comes as a string to Date and subtract one day from that date.

I did as follows:

dataConvertida = new Date("2018-07-03")

But when you do the conversion, it automatically discounts one day and needs to get the exact date.

    
asked by anonymous 02.07.2018 / 14:55

2 answers

0

Strange even as this happens. It should be something related to the time zone.

Anyway, would it be too much trouble if instead of "2018-07-03" you put "2018-07-03 00:00" ?

That's how it came out right (at least here)

    
02.07.2018 / 20:56
-1

There is the stepDown(qnt) method that reduces the number of days of the input type = date

<html>
<body>

<input type="date" id="data"> <!-- Pega a data --> 

<button onclick="removerUmDia()">Remover um dia</button> <!-- Botão que ativa a função, esse aqui dependerá de sua aplicação, como sua pergunta não especifica bem a utilização não tenho como ajudar mais que isso -->

<script>
function removerUmDia() {
    document.getElementById("data").stepDown(1); //Pega data e reduz 1 dia
}
</script>

</body>
</html>
    
02.07.2018 / 15:16