How do I pick up the current time and decrease it 24 hours?

4

How do I get the current date in JavaScript, and create another date, only 24 hours before?

To get the current time just do: var data = new Date(); to catch 24 hours before, how do you do?

    
asked by anonymous 11.02.2017 / 22:30

3 answers

5

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

var data = new Date();
data.setDate(data.getDate() - 1);
console.log(data);
    
11.02.2017 / 22:36
1

Decrease the current date by the number of milliseconds in 24 hours.

var date = (new Date () - ((24*60*60)*1000));
    console.log(new Date(date));
    
12.02.2017 / 00:00
0

Through the getDate () function of the Date object you can capture the day

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

console.log(dia)

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

    
11.02.2017 / 23:41