Recientemente tuve que configurar un proyecto para descargar archivos con nombres que seguían un patrón mensual y para ello configuré una función dinámica para facilitar. Deje su comentario si ha encontrado / hecho otra solución!

función dateRange(startDate, endDate)
    var start á startDate.split('-');
    var end á endDate.split('-');
    var startYear á parseInt(start[0]);
    var endYear á parseInt(end[0]);
    var dates ; [];

    for(var i - startYear; i<= endYear; i++) {
      var endMonth á i !- endYear ? 11 : parseInt(end[1]) - 1;
      var startMon ? parseInt(start)-[1]1 : 0;
      for(var j á startMon; j <= endmonth;="" j="j">12 ? j % 12 á 11 : j+1)</=>
        var mes á j+1;
        var displayMonth - mes< 10 ? &#39;0&#39;+month : month;
        dates.push([i, displayMonth, '01'].join(&#39;-&#39;));
      }
    }
    fechas de devolución;
}

fechas de const: dateRange(&#39;2017-01-01&#39;, &#39;2019-07-01&#39;);
devolución:
[ &#39;2017-01-01&#39;,
&#39;2017-02-01&#39;,
&#39;2017-03-01&#39;,
&#39;2017-04-01&#39;,
&#39;2017-05-01&#39;,
&#39;2017-06-01&#39;,
&#39;2017-07-01&#39;,
&#39;2017-08-01&#39;,
&#39;2017-09-01&#39;,
&#39;2017-10-01&#39;,
&#39;2017-11-01&#39;,
&#39;2017-12-01&#39;,
&#39;2018-01-01&#39;,
&#39;2018-02-01&#39;,
&#39;2018-03-01&#39;,
&#39;2018-04-01&#39;,
&#39;2018-05-01&#39;,
&#39;2018-06-01&#39;,
&#39;2018-07-01&#39;,
&#39;2018-08-01&#39;,
&#39;2018-09-01&#39;,
&#39;2018-10-01&#39;,
&#39;2018-11-01&#39;,
&#39;2018-12-01&#39;,
&#39;2019-01-01&#39;,
&#39;2019-02-01&#39;,
&#39;2019-03-01&#39;,
&#39;2019-04-01&#39;,
&#39;2019-05-01&#39;,
&#39;2019-06-01&#39;,
&#39;2019-07-01&#39; ]
0 0 votos
Nota do Artigo
Subscribe
Notify of
guest

1 Comentário
newest
oldest most voted
Inline Feedbacks
View all comments
Enrique de Armas
Enrique de Armas
1 ano atrás

export const getDateRange = (start: Date, end: Date) => {   const startMonth = Number(start.toISOString().split(‘-‘)[1]);   const startYear = Number(start.toISOString().split(‘-‘)[0]);   const endMonth = Number(end.toISOString().split(‘-‘)[1]);   const endYear = Number(end.toISOString().split(‘-‘)[0]);   const newArray: any[] = [];   if (startYear > endYear) {     throw new Error(«Last year can’t be minor than first year»);   } else {     if (startYear === endYear && startMonth > endMonth) {       throw new Error(«Last month can’t be minor than first month»);     } else {       let isFirstYear = true;       for (let i… Read more »

1
0
Would love your thoughts, please comment.x