How to take the current time and decrease 24 hours of it?

Asked

Viewed 638 times

4

How to take the current date in Javascript, and create another date, only 24 hours before?

To catch the current time just do: var data = new Date(); to pick up 24 hours before, as it does?

3 answers

5


You can use the .setDate and spend a day less than the current date, so it will change the internal date of that variable to what you want:

var data = new Date();
data.setDate(data.getDate() - 1);
console.log(data);

  • 1

    Exactly that.

1

Decrease the current date by the amount of milliseconds in 24h.

var date = (new Date () - ((24*60*60)*1000));
    console.log(new Date(date));

0

The getDate() function of the Date object captures the day

data = new Date();//cria o objeto
dia = data.getDate();//recupera o dia atual 

console.log(dia)

so it is positive to manipulate the variable day as you wish.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.