Sujitha
Sujitha

Reputation: 115

Decrement the date by one day using javascript for loop?

$(document).ready(function() { 
   var date = new Date();
   var data_new = [];var url ='http://www.domain.com /kjdshlka/api.php?date=2014-07-15'; 
   $.getJSON(url,function(result) { 
     var elt = [date,result.requests];data_new.push(elt);console.log(data_new);
   });
});

I am struggling to decrement the date by one day using javascript for loop.Here is my code,from the url im getting some requests.like if i decrease the date by one day other requests will come .Now i need this process for 7days using javascript for loop.Can anybody please tel me how to do ?

Upvotes: 0

Views: 1959

Answers (3)

chf
chf

Reputation: 763

 var now = new Date();
 console.log(now);

 var yesterday = new Date(now - 86400000);
 console.log(yesterday);

 /* In a Decrement Loop*/
 for(var i=100;i>0;i--){
    console.log(new Date(now - i*86400000));
 }

Upvotes: 0

Fezzy
Fezzy

Reputation: 64

var today = new Date();

var yesterday = new Date(today.getTime() - (24 * 60 * 60 * 1000)); //(hours * minutes * seconds * milliseconds)

console.log(yesterday);

Upvotes: 1

cp100
cp100

Reputation: 1493

var date = new Date(); // Date you want, here I got the current date and time
date.setDate(date.getDate()-1);

getDate() will give you the date, then reduce it by 1 and using setDate() you can replace date again.

Upvotes: 1

Related Questions