-1

I have an array with some dates in "dd/mm/yyyy" formatted as string.

var myDates = [ "01/04/2017", "11/12/2017", "12/09/2017", "02/03/2017" ];

1) How can I retrieve the dates between "01/02/2017" and "01/12/2017" only?

2) How can I convert a "dd/mm/yyyy" string to a date in the same format?

Regards, Elio Fernandes

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Elio Fernandes
  • 1,335
  • 2
  • 14
  • 29
  • 1
    Please ask one question at a time. And show some effort. You do not have 01/02/2017 ot 01/12/2017 in your array. It is also not clear if your array contains US or European dates – mplungjan Oct 08 '17 at 15:24

1 Answers1

0

Use Array#filter method to filter based on the required condition.

var myDates = ["01/04/2017", "11/12/2017", "12/09/2017", "02/03/2017"];

var start = new Date(2017, 2, 1), // start date
  end = new Date(2017, 12, 1); // end date



var res = myDates.filter(function(date) {
  // parse the date
  var d = new Date(...date.split('/').reverse());
  // or for older browser which doesn't support ES6 spread syntax
  // var dA = date.split('/');
  // var d = new Date(dA[2], dA[1], dA[0]);  
  
  // check the date is in between start and end
  return d >= start && d <= end;
});

console.log(res);
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188