Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8023953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:43:03+00:00 2026-06-04T22:43:03+00:00

I have the following code for generating a list of the next 12 months

  • 0

I have the following code for generating a list of the next 12 months (inclusive), starting from today’s date:

function DateUtilFunctions() {
    var self = this;

    var monthNames = new Array();

    monthNames[0] = "January";
    monthNames[1] = "February";
    monthNames[2] = "March";
    monthNames[3] = "April";
    monthNames[4] = "May";
    monthNames[5] = "June";
    monthNames[6] = "July";
    monthNames[7] = "August";
    monthNames[8] = "September";
    monthNames[9] = "October";
    monthNames[10] = "November";
    monthNames[11] = "December";

    self.getNext12MonthNamesWithYear = function () {
        var months = new Array();
        var today = new Date(Date());

        var loopDate = new Date();
        loopDate.setTime(today.valueOf());

        var todayPlus12Months = new Date(today.setMonth(today.getMonth() + 12));

        while (loopDate.valueOf() < todayPlus12Months.valueOf()) {
            alert(loopDate);
            alert(loopDate.getMonth());
            var month = monthNames[loopDate.getMonth()];


            months.push(month + ' ' + loopDate.getFullYear());
            loopDate.setMonth(loopDate.getMonth() + 1);
        }

        return months;
    };
}

The result of calling getNext12MonthNamesWithYear() is:

  • “May 2012”
  • “July 2012”
  • “August 2012”
  • “May 2012”
  • “July 2012”
  • “August 2012”
  • “September 2012”
  • “October 2012”
  • “November 2012”
  • “December 2012”
  • “January 2013”
  • “February 2013”
  • “March 2013”
  • “April 2013”
  • “May 2013”

As you can, the start of the list is a bit weird, in that “June” is missing, plus “May”, “July” and “August” appear twice.

Naturally I;m doing something very wrong here; could someone please help me out?

EDIT:

Based on micadelli’s comment, here is the solution I used:

function DateUtilFunctions() {
    var self = this;

    var monthNames = new Array();

    monthNames[0] = "January";
    monthNames[1] = "February";
    monthNames[2] = "March";
    monthNames[3] = "April";
    monthNames[4] = "May";
    monthNames[5] = "June";
    monthNames[6] = "July";
    monthNames[7] = "August";
    monthNames[8] = "September";
    monthNames[9] = "October";
    monthNames[10] = "November";
    monthNames[11] = "December";

    self.getNext12MonthNamesWithYear = function () {
        var months = new Array();
        var today = new Date();
        var tmpDate = new Date();
        var tmpYear = tmpDate.getFullYear();
        var tmpMonth = tmpDate.getMonth();
        var monthLiteral;

        for (var i = 0; i < 12; i++) {
            tmpDate.setMonth(tmpMonth + i);
            tmpDate.setFullYear(tmpYear);
            monthLiteral = monthNames[tmpMonth];

            months.push(monthLiteral + ' ' + tmpYear);

            tmpYear = (tmpMonth == 11) ? tmpYear + 1 : tmpYear;
            tmpMonth = (tmpMonth == 11) ? 0 : tmpMonth + 1;
        }

        return months;
    };
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T22:43:05+00:00Added an answer on June 4, 2026 at 10:43 pm

    I dont see any reason why this wouldn’t work

    function DateUtilFunctions() {
       var self = this;
    
       var monthNames = new Array();
    
       monthNames[0] = "January";
       monthNames[1] = "February";
       monthNames[2] = "March";
       monthNames[3] = "April";
       monthNames[4] = "May";
       monthNames[5] = "June";
       monthNames[6] = "July";
       monthNames[7] = "August";
       monthNames[8] = "September";
       monthNames[9] = "October";
       monthNames[10] = "November";
       monthNames[11] = "December";
    
       self.getNext12MonthNamesWithYear = function () {
         var months = new Array();
         var today = new Date();
         var tmpDate = new Date();
         var tmpYear = tmpDate.getFullYear();
         var tmpMonth = tmpDate.getMonth();
         var monthLiteral;
    
         for (var i = 0 ; i < 12 ; i++) {
            tmpDate.setMonth(tmpMonth + i);
            tmpDate.setFullYear(tmpYear);
            monthLiteral = monthNames[tmpMonth];
            months.push(monthLiteral + ' ' + tmpYear);
            tmpMonth = (tmpMonth == 11) ? 0 : tmpMonth+1;
            tmpYear = (tmpMonth == 11) ? tmpYear+1 : tmpYear;
         }
    
         return months;
       };
    }
    

    JS Bin
    http://jsbin.com/aqezom

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Generating random numbers in Javascript I have the following code var randomnumber=Math.floor(Math.random()*101);
I have following code for loading image from url in xml parsing endElement method
I have following code that is generating random no. between 1 to 10. This
So I have the following code with works great for randomly generating position for
I have the following code (taken from actual script): $result=mysql_query($sql); while($row=mysql_fetch_assoc($result)) { $auth_token=$row['oauth_token']; $auth_token_secret=$row['oauth_token_secret'];
I have the following code generating content on my site: <div class=content id=links> <?php
I have the following code that is generating an error when I try to
I have following code working on a development environment (SQLite) but generating an error
I have used this web link as reference . The following code is generating
I have the following code: public function ajax() { // Contains results $data =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.