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

134
Views
Generate array/object of years and months between two dates

I am trying to generate a dataset of years, each containing an array of months, for two given dates. From a given start date, say 15.05.2010, to 03.02.2012 this set should contain {'2010': ['May', 'June', 'July' ...] ... } up to {'2012': ['January', 'February']}.

I am in a situation where I cannot use libraries, I am confined to vanilla. I can generate a set of years but I am failing to add the months. Here's what I've tried so far:

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

I need to iterate over all years and then iterate over each month in a given year, so for each year, I need to know which months where in there.

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

This comment by @CBroe gave me the answer I needed. This is my solution now:

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!