How can you get an array of dates from a string in the simplest way possible? For example, from ['15.02.2016','16.02.2016','17.02.2016'] to make ['15.02.2016','16.02.2016','17.02.2016']

  • date format defined? - Grundy
  • yes, it is in this form dmY in php terminology) - Ordman
  • then: split by - , translation of lines into dates and the usual cycle - Grundy
  • the difficulty is that it is not quite clear how to increment dates, especially if the period from the middle of the month to the middle of the next month is Ordman
  • In the array, only days should be? - Grundy

1 answer 1

 function fn(str) { str = str.split(/-|\./); var arr = [], from = new Date(str[2], str[1] - 1, str[0]), to = new Date(str[5], str[4] - 1, str[3]); for (; from <= to;) { arr.push((from.getDate() + "." + (from.getMonth() + 1) + "." + from.getFullYear()).replace(/(^|\.)(?=\d\.)/g, "$10")); from.setDate(from.getDate() + 1); } return arr; } document.write(JSON.stringify(fn('15.02.2016-17.02.2016')));