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