Home > Software design >  How to get next months and previous months using the given short month in javascript
How to get next months and previous months using the given short month in javascript

Time:11-23

a month is given in short notation. Month="DEC" I just wanted to get the coming 8 months and previous 3 months of DEC in some arrays like below.

nextMonths =["JAN","FEB", "MAR", "APR", "MAY","JUN","JUL","AUG"]

And

PrevMonths =["NOV","OCT", "SEP"] . Expecting an easy solution in JavaScript.

CodePudding user response:

assuming that you have some array with all the months

const months = ["JAN","FEB", "MAR", "APR", "MAY","JUN","JUL","AUG","SEP", "OCT", "NOV", "DEC"]


const nextMonths = (month, number) => {
  const index = months.indexOf(month)
  return Array(number).fill(0).map((_, i) => months[(index   i  1) % 12])
}

const prevMonths = (month, number) => {
   const index = months.indexOf(month)
   return Array(number).fill(0).map((_, i) => months[(12   index - i - 1) % 12])
}

console.log(nextMonths("DEC", 8))
console.log(prevMonths("DEC", 3))
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

Here I'm taking advantage of the fact that you don't appear to care about localization, and that we're unlikely to change the existence or order of months anytime soon. I'm also being lazy and checking a two-year range instead of using modular arithmetic:

const months = ["JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"]

let getNextEightMonths = (m) => {
  let curMonthIndex = months.indexOf(m);
  if (curMonthIndex == -1) {return false} // reject bad input
  let range = months.concat(months) // doubling the array since the range can span into next year
  return range.splice(curMonthIndex 1,8)
}

let getPrecedingThreeMonths = (m) => {
  let curMonthIndex = months.indexOf(m);
  if (curMonthIndex == -1) {return false} 
  let range = months.concat(months) 
  return range.splice(curMonthIndex 9,3)
}  

  
console.log(getNextEightMonths("JAN"));
console.log(getPrecedingThreeMonths("JAN"))
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

Hey you can do it with moment

const upcomingMonths = (statingdate) => {
 const months = []
 const dateStart = moment(statingdate) // put your start date here 
 const dateEnd1 = moment().add(8, ‘month')
 while (dateEnd1.diff(dateStart, ‘months') >= 0) {
  months.push(dateStart.format(‘MMM'))
  dateStart.add(1, ‘month')
 }
 return months
}

const PrevMonths = (statingdate) => {
 const months = []
 const dateStart = moment(statingdate) // put your start date here 
 const dateEnd = moment().subtract(3, ‘month')
while (dateEnd.diff(dateStart, ‘months') >= 0) {
  months.push(dateStart.format(‘MMM'))
  dateStart.subtract(1, ‘month')
 }
 return months
}

Just Let ME know if it helps

CodePudding user response:

You can handle it using a fixed array and iterating pushing into a couple of arrays what was before and after you index. It could also be achieved using map instead of a for cycle.

const months = ["JAN","FEB", "MAR", "APR", "MAY","JUN","JUL","AUG", "SEP", "OCT", "NOV", "DEC"];
            let prev = [];
            let follow =[];
            let found =false;
        
            for(let m in months){
              if(months[m] === current){  
                found = true;
              }
              if(found == true){
                follow.push(months[m]);
              }
              else{
                prev.push(months[m])
              }
            }
        
            console.log(prev);
            console.log(follow);

CodePudding user response:

Maybe not super elegant, but

const getMonthsByShortCode = code => {
    const months = ["JAN","FEB", "MAR", "APR", "MAY","JUN","JUL","AUG",'SEP','OCT','NOV','DEC'];
    const getByPosition = (start,end) => 
        months.concat(months).slice(start,end).filter(x => x != code);
    let index = months.findIndex(x => x == code);
    return {
        prevMonths: getByPosition(index 12-3,index   12),
        nextMonths: getByPosition(index,index   8)
    };
};
console.log(getMonthsByShortCode('DEC'));
console.log(getMonthsByShortCode('JAN'));
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

Assuming that you have an array with the ordered months:

const orderedMonths = ["JAN","FEB", "MAR", "APR", "MAY","JUN","JUL","AUG", "SEP", "OCT", "NOV", "DEC"];

You could do something like

const orderedMonths = ["JAN","FEB", "MAR", "APR", "MAY","JUN","JUL","AUG", "SEP", "OCT", "NOV", "DEC"];


const getPrevAndNextMonths = (month) => {
        const doubledMonths = orderedMonths.concat(orderedMonths);
        // This creates an array that goes from january to december twice
        const index = orderedMonths.indexOf(month);
        const prevIndex = index   12;
        const prevMonths = doubledMonths.slice(prevIndex - 3, prevIndex);
        const nextMonths = doubledMonths.slice(index   1, index   9);
        return { prevMonths, nextMonths };
    }
    
    console.log(getPrevAndNextMonths("MAR"));
<iframe name="sif4" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related