-2

I have 10 value like this ( I use new Date() to create it ) :

Wed Jun 14 2017 18:51:33 
Wed Feb 7 2017 18:51:33 
Wed Apr 10 2017 18:51:33 
Wed Jun 10 2017 18:51:33 
Wed Jun 1 2017 18:51:33 
.... 

How can I get value of last 5 day

2 Answers2

7

you can get past dates by decrementing date you get from new Date() function.

check below code.

var yesterday = new Date();
yesterday.setDate(yesterday.getDate() - 1);

you can do date formatting in yesterday too.

Bhaumik Pandhi
  • 2,655
  • 2
  • 21
  • 38
1

In Javascript,

add dates to one array, iterate them

var datesArray = []; //add dates to this array
var lastfivedays = [];

datesArray.forEach(function(checkdate){
  var currentDate = new Date();
  var timeDiff = currentDate.getTime() - checkdate.getTime();
  var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24)); 
  if(diffDays == -5){ 
    lastfivedays.push(checkdate)  
  } 
});

lastfivedays array contains dates of last 5 day

Arjun
  • 2,159
  • 1
  • 17
  • 26