Am looking for something where I could get a array of next three months in number from the current month , i have tried to get the month in string but looking for something like eg : current month is 11 (ie) November so it should return 12,1,2 as next three months , any links would be great. TIA

Dani AI

Generated

You can get the next three month numbers without touching date arithmetic at all. That avoids the classic end-of-month rollover (e.g., Jan 31 + 1 month jumping into March) that can happen with setMonth, which is the only pitfall in ’s otherwise solid note about 0..11 indexing. Also, as ’s mapping hints, watch the modulo edge case: (currentMonth + n) % 12 can yield 0, which you must remap to 12 for conventional month numbers.

// Returns next N months as 1..12 numbers, wrapping across years.
function nextMonths(count = 3, start = new Date()) {
  const m0 = start.getMonth(); // 0..11
  return Array.from({ length: count }, (_, i) => ((m0 + i + 1) % 12) || 12);
}

// Examples
nextMonths();    // if current month is 11 (Nov) -> [12, 1, 2]
nextMonths(5);   // get more than three if needed

If you also want labels:

const names = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
const nums = nextMonths();                // [12, 1, 2]
const labels = nums.map(n => names[n-1]); // ["Dec","Jan","Feb"]

Notes:

  • Need UTC instead of local time? Replace getMonth() with getUTCMonth().
  • ’s wraparound array trick works too; using the modulo keeps it concise without extra copying.
  • If you ever pass in a manual starting month, work with 0..11 internally and convert back to 1..12 at the end to avoid off-by-one bugs.

Recommended Answers

All 3 Replies

Given you want the next three months I see two quick solutions.

  1. Math. In psuedo code:
    cm = 11 'current month
    array[1] = cm +1;
    array[2] = array[1]+1;
    array[3] = array[2]+2;
    for i = 1 to 3; if array[i]>12 then array[i]--12;

  2. Another method but with some array copy.
    marray[]= 1,2,3,4,5,6,7,8,9,10,11,12,1,2,3
    arraycopy(array[1] (from), marray[cm], 3 locations)

My answer here is messy and in psuedocode. You can tailor it to fit or create your own solution.

In JS months numbered from 0 to 11. Actually 11 is december. You can write simple function

function addMonths(d,n){
    var dt=new Date(d.getTime());
    dt.setMonth(dt.getMonth()+n);
    return dt;
}

and then e.g.

var d=new Date(); // current date
var d1=addMonths(d,1);
var d2=addMonths(d,2);
var d3=addMonths(d,3);

convert month number to 1-12

var n1=d1.getMonth()+1;
var n2=d2.getMonth()+1;
var n3=d3.getMonth()+1;

Pretty simple, start with array [1, 2, 3] (representing offsets from current month) and map it to the array you want.

function getNext3Months() {
    const currentMonth = (new Date()).getMonth() + 1; // getMonth() is zero-indexed; add 1 to give conventional month number.
    return [1, 2, 3].map(n => (currentMonth + n) % 12); // returns array of next three conventional month numbers; %12 caters for end of year wrap-around.
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.