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

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());
    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, learning, and sharing knowledge.