0
$(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 ?

Siamak Motlagh
  • 5,028
  • 7
  • 41
  • 65
Sujitha
  • 115
  • 1
  • 1
  • 6

3 Answers3

1
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.

cp100
  • 1,463
  • 6
  • 20
  • 35
1

var today = new Date();

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

console.log(yesterday);

Fezzy
  • 64
  • 2
  • Hi fezzy , thanks for your reply .But i need to decrease the date by one day for one week . for that i need for loop fezzy . – Sujitha Jul 16 '14 at 06:53
0
 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));
 }
chf
  • 743
  • 4
  • 14