Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

122
Views
Generar matriz/objeto de años y meses entre dos fechas

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í.

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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);
about 4 years ago · Santiago Trujillo Report

0

 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);

about 4 years ago · Santiago Trujillo Report

0

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

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!