Estoy tratando de generar un conjunto de datos de años, cada uno con una matriz de meses, para dos fechas determinadas. Desde una fecha de inicio dada, digamos 15.05.2010 , hasta 03.02.2012 , este conjunto debe contener {'2010': ['May', 'June', 'July' ...] ... } hasta {'2012': ['January', 'February']} .
Estoy en una situación en la que no puedo usar bibliotecas, estoy confinado a vanilla. Puedo generar un conjunto de años pero no puedo sumar los meses. Esto es lo que he intentado hasta ahora:
const startDate = new Date('2005-01-01'); const today = new Date(); // const difference = today.getFullYear() - startFrom.getFullYear(); const years = []; const lastYear = today.getFullYear() - 1; const yearsAndMonths = {}; let startFrom = startDate; // set all the years while (startFrom.setFullYear(startFrom.getFullYear() + 1) < today.setFullYear(lastYear)) { years.push(startFrom.toLocaleDateString('de-AT', { year: 'numeric', })); } startFrom.setFullYear(startDate); while (startFrom.setMonth(startFrom.getMonth() + 1) < today) { const year = startFrom.getFullYear(); const yearIndex = yearsAndMonths.indexOf(year); yearsAndMonths[year].push(startFrom.toLocaleDateString('de-AT', { month: 'long' })); } console.log(yearsAndMonths);https://codepen.io/thomassemmler/pen/poLBdgR?editors=0011
Necesito iterar sobre todos los años y luego iterar sobre cada mes en un año determinado, por lo que para cada año, necesito saber qué meses están ahí.
let months = [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ]; let startDate = new Date("05/01/2005"); let todays = new Date(); let startDateYear = startDate.getFullYear(); let currentYear = todays.getFullYear(); let dataSet = {} for (let i = startDateYear; i <= currentYear; i++) { if (i === startDateYear) { let monthId = startDate.getMonth(); dataSet = Object.assign({ [i]: months.slice(monthId, months.length) }, dataSet); } else if (i === currentYear) { let monthId = todays.getMonth(); dataSet = Object.assign({ [i]: months.slice(0, monthId + 1) }, dataSet); } else { dataSet = Object.assign({ [i]: months }, dataSet); } } console.log(dataSet); const startFrom = new Date('2005-05-01'); const today = new Date(); const yearsAndMonths = {}; do { const year = startFrom.getFullYear(); const month = startFrom.toLocaleDateString('de-AT', {month: 'long'}); if (yearsAndMonths[year]) yearsAndMonths[year].push(month); else yearsAndMonths[year] = [month]; } while (startFrom.setMonth(startFrom.getMonth() + 1) < today); console.log(yearsAndMonths);Este comentario de @CBroe me dio la respuesta que necesitaba. Esta es mi solución ahora:
const startDate = new Date('2005-01-01'); const today = new Date(); let year = startDate.getFullYear(); let month = startDate.getMonth() + 1; const endYear = today.getFullYear(); const endMonth = today.getMonth() + 1; let result = {}; while(year < endYear || year == endYear && month <= endMonth) { const date = new Date(); date.setMonth(month); result[year] = result[year] ? result[year] : [] result[year].push(date.toLocaleString('de-AT', {month: 'long'})) month++ if (month > 12) { month = 1 year++ } } console.log(result)Codepen: https://codepen.io/thomassemmler/pen/bGvJaRL?editors=0011