Js partly reduce two arrays

I have two arrays:
arr1 = [60,70,80,80,100,110,120,130,140,150,100,120]

arr2 = [60,70,80,80,100,110,120,130,140,150,100,120]

I want to count those arrays, but… in case the initial mont in my script is 8, array1 has to count from index 7 till 11 and array2 count index 0 till 6. The result of these two must also be counted. The result i suppose concat?

The initial month can differ.

Just for month 8, or do you mean for every month X you want to start counting from (X - 1) to 11 from arr1 and 0 to (X - 2) from arr2?

Yes i want to count index 7,8,9,10 and 11 from array1 and 0,1,2,3,4,5 and 6 from array2. And count those two results

But only for month 8? What should happen for the other months?

Array1are the monthly results of the peak performance from my solar panels in 2022. Array2 are the monthly results of the peak performance from my solar panels in 2023. The energy company bills from august-july. So i want the monthly peak performance from august 2022 till july 2023. This also means in december there is no array2. Array2 will be made on 1 janauary 2023.

Is this going the right direction, assuming that “global.get(‘energyContractYear’);” has been set the first time to currentYear with "global.set(‘energyContractYear’, +currentYear)

var energyContractMonth = '8';

global.get('energyContractYear');

if(energyContractYear === currentYear)
{
var tmpPeakContractPeriodMonth = global.get('peakContractPeriodMonth' +currentYear);
var tmp = tmpPeakContractPeriodMonth.slice(energyContractMonth -1,tmpPeakContractPeriodMonth.length);
global.set('PeakContractPeriodMonth' +currentYear, tmp);
var tmp1 = tmp.reduce((partialSum, a) => partialSum + a, 0);
peakContractPeriodYear.splice(currentYear -1,1,tmp1);
global.set('PeakContractPeriodYear', peakContractPeriodYear);
}
{
var tmpPeakContractPeriodMonth = global.get('peakContractPeriodMonth' +currentYear);
var tmp = tmpPeakContractPeriodMonth.slice(0,energyContractMonth -2);
global.set('peakContractPeriodMonth' +currentYear, tmp);
var tmp1 = tmp.reduce((partialSum, a) => partialSum + a, 0);
tmp2 = global.get('PeakContractPeriodYear');
var tmp2 = tmp1 + tmp2[currentYear -2];
peakContractPeriodYear.splice(currentYear -2,1,tmp2);
global.set('peakContractPeriodYear', peakContractPeriodYear);
}

if(currentmonth -1 ===  energyContractMonth - 2)
{
global.set('energyContractYear', +currentYear)
}

This how far i am. Looks difficult, to me. Maybe there is an easier way?

var indexMonth = [1,2,3,4,5,6,7,8,9,10,11,12,1,2,3,4,5,6,7,8,9,10,11,12]

var indexYear = [currentYear -1,currentYear -1,currentYear -1,currentYear -1,currentYear -1,currentYear -1,currentYear -1,currentYear -1,currentYear -1,currentYear -1,currentYear -1,currentYear -1,currentYear,currentYear,currentYear,currentYear,currentYear,currentYear,currentYear,currentYear,currentYear,currentYear,currentYear,currentYear,]

var daysInCurrentMonth = getDaysInMonth(+indexYear[(currentMonth -1) +12], +indexMonth[(currentMonth -1) +12]);
console.log('49 days in currentMonth is: '+daysInCurrentMonth)
var indexThisMonthDay = Array.from({length: +daysInCurrentMonth}, (_, i) => i + 1)
console.log('50 indexThisMonthDay is: '+indexThisMonthDay)

var daysInPreviousMonth = getDaysInMonth(+indexYear[(currentMonth -2) +12], +indexMonth[(currentMonth -2) +12]);
console.log('51 days in previousMonth is: '+daysInPreviousMonth)

indexPreviousMonthDay = Array.from({length: +daysInPreviousMonth}, (_, i) => i + 1)
console.log('52 indexPreviousMonthDay is: '+indexPreviousMonthDay)

var indexDay = indexPreviousMonthDay.concat(indexThisMonthDay);
console.log('53 indexDay is: '+indexDay)
console.log('54 daysInCurrentMonth is: '+daysInCurrentMonth)

var tmpPeakDay = global.get('peakDay'  +indexYear[((currentMonth -1) +12)] +indexMonth[((currentMonth -1) +12)]);
tmpPeakDay.splice(+indexDay[((+daysInPreviousMonth -1) +currentDay -1)],1,peak);
global.set('peakDay'+indexYear[((currentMonth -1) +12)] +indexMonth[((currentMonth -1) +12)], tmpPeakDay);
console.log('67 peakDay'+indexYear[((currentMonth -1) +12)] +indexMonth[((currentMonth -1) +12)] +' is ' +tmpPeakDay);
var peakDay = tmpPeakDay[currentDay - 1];
console.log('68 peakDay is ' +peakDay);
var tmpPeakPreviousDay = global.get('peakDay' +indexYear[((currentMonth -2) +12)] +indexMonth[((currentMonth -2) +12)]);
console.log('69 tmpPeakPreviousDay' +indexYear[((currentMonth -2) +12)] +indexMonth[((currentMonth -2) +12)] +' is ' +tmpPeakPreviousDay);
var peakPreviousDay = tmpPeakPreviousDay[+indexDay[(((+daysInPreviousMonth -1) +currentDay -1) -1)]];
console.log('70 peakPreviousDay is ' +peakPreviousDay);
var peakDayResult = peakDay -peakPreviousDay
console.log('71 peakDayResult is: ' +peakPreviousDay +' - ' +peakDay +' is ' +tmpPeakDay);


var tmpPeakMonth = global.get('peakMonth' +indexYear[((currentMonth -1) +12)]);
console.log('72 tmpPeakMonth'+indexYear[((currentMonth -1) +12)] +' is: ' +tmpPeakMonth);
var tmpPeakPreviousMonth = global.get('peakMonth' +indexYear[((currentMonth -2) +12)]);
console.log('73 tmpPeakPreviousMonth'+indexYear[((currentMonth -1) +12)] +' is: ' +tmpPeakPreviousMonth);
tmpPeakMonth.splice(+indexYear[((currentMonth -1) +12)],1,peakDay - tmpPeakPreviousMonth[+indexMonth[((currentMonth -2) +12)]]);
global.set('peakMonth'+indexYear[((currentMonth -1) +12)], tmpPeakMonth);
console.log('74 peakMonth' +indexYear[((currentMonth -1) +12)]  +' is ' +tmpPeakMonth);


var energyContractMonth = '8';

var energyContractYear = global.get('energyContractYear');
console.log('77 energyContractYear is: '+energyContractYear)
if(energyContractYear === currentYear)
{
var tmpPeakContractPeriodYear = global.get('peakContractPeriodYear')
var tmpPeakContractPeriodMonth = global.get('peakContractPeriodMonth' +currentYear);
var tmp = tmpPeakContractPeriodMonth.slice(energyContractMonth -1,tmpPeakContractPeriodMonth.length);
global.set('PeakContractPeriodMonth' +currentYear, tmp);
var tmp1 = tmp.reduce((partialSum, a) => partialSum + a, 0);
tmpPeakContractPeriodYear.splice(currentYear -1,1,tmp1);
global.set('PeakContractPeriodYear', tmpPeakContractPeriodYear);
}
{
var tmpPeakContractPeriodYear = global.get('peakContractPeriodYear')
var tmpPeakContractPeriodMonth = global.get('peakContractPeriodMonth' +currentYear);
var tmp = tmpPeakContractPeriodMonth.slice(0,energyContractMonth -2);
global.set('peakContractPeriodMonth' +currentYear, tmp);
var tmp1 = tmp.reduce((partialSum, a) => partialSum + a, 0);
tmp2 = global.get('PeakContractPeriodYear');
var tmp2 = tmp1 + tmp2[currentYear -2];
tmpPeakContractPeriodYear.splice(currentYear -2,1,tmp2);
global.set('peakContractPeriodYear', tmpPeakContractPeriodYear);
}

var tmpPeakYear = global.get('peakYear');
var tmpPeakMonth = global.get('peakMonth'+indexYear[((currentMonth -1) +12)]);
var tmp = tmpPeakMonth.reduce((partialSum, a) => partialSum + a, 0);
tmpPeakYear.splice(+indexYear[((currentMonth -1) +12)] -2001,1,tmp);
global.set('peakYear',tmpPeakYear );
console.log('78 peakYear is ' +tmpPeakYear);

if(currentMonth -1 === energyContractMonth - 2)
{
global.set('energyContractYear', +currentYear)
}

tmpPeakContractPeriodYear.splice(currentYear -2001,1,meterKwhThisYear_PBTH);
tmpPeakContractPeriodYear.splice(currentYear -2002,1,meterKwhPreviousYear_PBTH);
global.set('peakContractPeriodYear', tmpPeakContractPeriodYear);
console.log('77 peakContractPeriodYear is ' +tmpPeakContractPeriodYear);



Think i have the solution. Added some variables with the day, month and year when it is yesterday and the day, month and year when it is the previous month.

var date = new Date();
date.setDate(date.getDate());
const currentDay = date.getDate();
console.log('current day is: ' +currentDay)

date.setDate(date.getDate() -(+currentDay));
const lastDayPreviousMonth = date.getDate();
console.log('lastDayPreviousMonth is: '+lastDayPreviousMonth)
const previousMonth = date.getMonth(); // getMonth() returns month from 0 to 11
console.log('previousMonth is: '+previousMonth)
const yearPreviousMonth = date.getFullYear();
console.log('yearPreviousMonth is: '+yearPreviousMonth)

var date = new Date();
date.setDate(date.getDate());
console.log('current day is: ' +currentDay)
const currentMonth = date.getMonth(); // getMonth() returns month from 0 to 11
console.log('current month is: '+(currentMonth +1))
const currentYear = date.getFullYear();
console.log('current year is: '+currentYear)


function getDaysInMonth(year, month) {
  return new Date(year, month, 0).getDate();

}
const daysInCurrentMonth = getDaysInMonth(currentYear, currentMonth +1);
console.log('11 daysInCurrentMonth is: '+daysInCurrentMonth)

var tmpPeakDay = global.get('peakDay' +currentYear +(currentMonth +1));
tmpPeakDay.splice(currentDay -1,1,peak);
global.set('peakDay'+currentYear +(currentMonth +1), tmpPeakDay);
console.log('67 peakDay'+currentYear +(currentMonth +1) +' is ' +tmpPeakDay);
var peakDay = tmpPeakDay[currentDay - 1];
console.log('68 peakDay is ' +peakDay);
var tmpPeakPreviousDay = global.get('peakDay' +yearMinusOneDay +(monthMinusOneDay +1));
console.log('69 tmpPeakPreviousDay' +yearMinusOneDay +(monthMinusOneDay +1) +' is ' +tmpPeakPreviousDay);
var peakPreviousDay = tmpPeakPreviousDay[dayMinusOneDay -1];
console.log('70 peakPreviousDay day '+[dayMinusOneDay -1] +' is: '+peakPreviousDay);
var peakDayResult = peakDay -peakPreviousDay
console.log('71 peakDayResult is: ' +peakDay +' - ' +peakPreviousDay +' is ' +peakDayResult);

var tmpPeakMonth = global.get('peakMonth' +currentYear);
console.log('72 tmpPeakMonth'+currentYear +' is: ' +tmpPeakMonth);
var tmpPeakPreviousMonth = global.get('peakMonth' +yearPreviousMonth);
console.log('73 tmpPeakPreviousMonth' +yearPreviousMonth +' is: ' +tmpPeakPreviousMonth);
tmpPeakMonth.splice(currentMonth,1,peakDay - lastDayPreviousMonth);
global.set('peakMonth'+currentYear, tmpPeakMonth);
console.log('74 peakMonth' +currentYear  +' is ' +tmpPeakMonth);


var energyContractMonth = '8';

var energyContractYear = global.get('energyContractYear');
console.log('77 energyContractYear is: '+energyContractYear);
if(energyContractYear === currentYear)
{
var tmpPeakContractPeriodYear = global.get('peakContractPeriodYear');
console.log('78 tmpPeakContractPeriodYear is: '+tmpPeakContractPeriodYear);
var tmpPeakContractPeriodMonth = global.get('peakMonth' +currentYear);
console.log('79 tmpPeakContractPeriodMonth' +currentYear +' is: '+tmpPeakContractPeriodMonth);
var tmp = tmpPeakContractPeriodMonth.slice(energyContractMonth -1,tmpPeakContractPeriodMonth.length);
console.log('80 tmpPeakContractPeriodMonth' +currentYear +' is: '+tmp);
global.set('peakContractPeriodMonth' +currentYear, tmp);
var tmp1 = tmp.reduce((partialSum, a) => partialSum + a, 0);
tmpPeakContractPeriodYear.splice(currentYear -2001,1,tmp1);
console.log('81 tmpPeakContractPeriodYear is: '+tmpPeakContractPeriodYear);
global.set('peakContractPeriodYear', tmpPeakContractPeriodYear);
}
{
var tmpPeakContractPeriodYear = global.get('peakContractPeriodYear')
var tmpPeakContractPeriodMonth = global.get('peakContractPeriodMonth' +(currentYear -1));
var tmp = tmpPeakContractPeriodMonth.slice(0,energyContractMonth -2);
global.set('peakContractPeriodMonth' +(currentYear -1), tmp);
var tmp1 = tmp.reduce((partialSum, a) => partialSum + a, 0);
var tmp2 = tmpPeakContractPeriodYear.slice((currentYear -1) -2001,tmp1);
global.set('peakContractPeriodYear', tmp2);
}


if(currentMonth -1 === energyContractMonth - 2)
{
global.set('energyContractYear', +currentYear)
}


Hope this will work