I have a drop-down menu with options such as "Last 24 hours", "Last 48 hours", etc. When I select "Last 24 hours" from the drop-down, I would like to retrieve all dates from now until yesterday in one-hour intervals.
Here is my attempt at achieving this:
var todayDate = new Date();
if(type=="hours"){ // for hours-based drop-down
var oneDayAgo = new Date(todayDate.getTime());
oneDayAgo.setDate(todayDate.getDate() - 1);
console.log("oneDayAgo"+oneDayAgo);
var hours = todayDate.getHours();
for(var i = hours; i <= hours+24; i++) {
if(i<25){
var newHours=i;
var newDates=todayDate.getFullYear() + "-" + ("00" + (todayDate.getMonth() + 1)).slice(-2) + "-" + ("00" + todayDate.getDate()).slice(-2) + " " + ("00" +newHours).slice(-2) + ":" + ("00" + todayDate.getMinutes()).slice(-2) + ":" + ("00" + todayDate.getSeconds()).slice(-2);
console.log(newDates);
}else{
var newHours=i-24;
var newDates=oneDayAgo.getFullYear() + "-" + ("00" + (oneDayAgo.getMonth() + 1)).slice(-2) + "-" + ("00" + oneDayAgo.getDate()).slice(-2) + " " + ("00" +newHours).slice(-2) + ":" + ("00" + oneDayAgo.getMinutes()).slice(-2) + ":" + ("00" + oneDayAgo.getSeconds()).slice(-2);
console.log(newDates);
}
}
}
My desired output is as follows:
If the current date and time are 2014-04-27 13:07, then the expected output should be: 2014-04-27 13:07, 2014-04-27 12:07, 2014-04-27 11:07 , 2014-04-27 10:07.... 2014-04-26 13:07
Any help or suggestions on how to achieve this would be greatly appreciated. Thank you.