Last 12 months in Javascript












3














I got an Kendo ui Chart like this one and have to show the last 12 months of todays date on the axis.

I found this to extend the date object to get the previous month. The problem seems to be when I got an Date like "2013/05/31" and the previous months got no 31st day.



Date.prototype.toPrevMonth = function (num) {
var thisMonth = this.getMonth();
this.setMonth(thisMonth-1);
if(this.getMonth() != thisMonth-1 && (this.getMonth() != 11 || (thisMonth == 11 && this.getDate() == 1)))
this.setDate(0);
}


new Date().toPrevMonth(11),
new Date().toPrevMonth(10),
new Date().toPrevMonth(9),
new Date().toPrevMonth(8),
new Date().toPrevMonth(7),
new Date().toPrevMonth(6),
new Date().toPrevMonth(5),
new Date().toPrevMonth(4),
new Date().toPrevMonth(3),
new Date().toPrevMonth(2),
new Date().toPrevMonth(1),
new Date().toPrevMonth(0)


Can anyone help me out with the if state?
The function is build to show only the previous month, but I need the last 12 previous months.



Or is there a much easier solution? :-)



Thanks for all!










share|improve this question



























    3














    I got an Kendo ui Chart like this one and have to show the last 12 months of todays date on the axis.

    I found this to extend the date object to get the previous month. The problem seems to be when I got an Date like "2013/05/31" and the previous months got no 31st day.



    Date.prototype.toPrevMonth = function (num) {
    var thisMonth = this.getMonth();
    this.setMonth(thisMonth-1);
    if(this.getMonth() != thisMonth-1 && (this.getMonth() != 11 || (thisMonth == 11 && this.getDate() == 1)))
    this.setDate(0);
    }


    new Date().toPrevMonth(11),
    new Date().toPrevMonth(10),
    new Date().toPrevMonth(9),
    new Date().toPrevMonth(8),
    new Date().toPrevMonth(7),
    new Date().toPrevMonth(6),
    new Date().toPrevMonth(5),
    new Date().toPrevMonth(4),
    new Date().toPrevMonth(3),
    new Date().toPrevMonth(2),
    new Date().toPrevMonth(1),
    new Date().toPrevMonth(0)


    Can anyone help me out with the if state?
    The function is build to show only the previous month, but I need the last 12 previous months.



    Or is there a much easier solution? :-)



    Thanks for all!










    share|improve this question

























      3












      3








      3







      I got an Kendo ui Chart like this one and have to show the last 12 months of todays date on the axis.

      I found this to extend the date object to get the previous month. The problem seems to be when I got an Date like "2013/05/31" and the previous months got no 31st day.



      Date.prototype.toPrevMonth = function (num) {
      var thisMonth = this.getMonth();
      this.setMonth(thisMonth-1);
      if(this.getMonth() != thisMonth-1 && (this.getMonth() != 11 || (thisMonth == 11 && this.getDate() == 1)))
      this.setDate(0);
      }


      new Date().toPrevMonth(11),
      new Date().toPrevMonth(10),
      new Date().toPrevMonth(9),
      new Date().toPrevMonth(8),
      new Date().toPrevMonth(7),
      new Date().toPrevMonth(6),
      new Date().toPrevMonth(5),
      new Date().toPrevMonth(4),
      new Date().toPrevMonth(3),
      new Date().toPrevMonth(2),
      new Date().toPrevMonth(1),
      new Date().toPrevMonth(0)


      Can anyone help me out with the if state?
      The function is build to show only the previous month, but I need the last 12 previous months.



      Or is there a much easier solution? :-)



      Thanks for all!










      share|improve this question













      I got an Kendo ui Chart like this one and have to show the last 12 months of todays date on the axis.

      I found this to extend the date object to get the previous month. The problem seems to be when I got an Date like "2013/05/31" and the previous months got no 31st day.



      Date.prototype.toPrevMonth = function (num) {
      var thisMonth = this.getMonth();
      this.setMonth(thisMonth-1);
      if(this.getMonth() != thisMonth-1 && (this.getMonth() != 11 || (thisMonth == 11 && this.getDate() == 1)))
      this.setDate(0);
      }


      new Date().toPrevMonth(11),
      new Date().toPrevMonth(10),
      new Date().toPrevMonth(9),
      new Date().toPrevMonth(8),
      new Date().toPrevMonth(7),
      new Date().toPrevMonth(6),
      new Date().toPrevMonth(5),
      new Date().toPrevMonth(4),
      new Date().toPrevMonth(3),
      new Date().toPrevMonth(2),
      new Date().toPrevMonth(1),
      new Date().toPrevMonth(0)


      Can anyone help me out with the if state?
      The function is build to show only the previous month, but I need the last 12 previous months.



      Or is there a much easier solution? :-)



      Thanks for all!







      javascript date kendo-ui






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Sep 26 '13 at 6:40









      chris

      2,79242651




      2,79242651
























          3 Answers
          3






          active

          oldest

          votes


















          3














          Use Datejs (http://www.datejs.com/)



          It has a built in function to add months:



          Date.today().addMonths(-6);


          UPDATE:
          Since you're not able to include external files, here are the relevant methods from within Datejs.



          /*
          * Copyright (c) 2006-2007, Coolite Inc. (http://www.coolite.com/). All rights reserved.
          * License: Licensed under The MIT License. See license.txt and http://www.datejs.com/license/.
          * Website: http://www.datejs.com/ or http://www.coolite.com/datejs/
          */

          Date.isLeapYear = function (year) {
          return (((year % 4 === 0) && (year % 100 !== 0)) || (year % 400 === 0));
          };

          Date.prototype.isLeapYear = function () {
          var y = this.getFullYear();
          return (((y % 4 === 0) && (y % 100 !== 0)) || (y % 400 === 0));
          };

          Date.getDaysInMonth = function (year, month) {
          return [31, (Date.isLeapYear(year) ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month];
          };

          Date.prototype.getDaysInMonth = function () {
          return Date.getDaysInMonth(this.getFullYear(), this.getMonth());
          };

          Date.prototype.addMonths = function (value) {
          var n = this.getDate();
          this.setDate(1);
          this.setMonth(this.getMonth() + value);
          this.setDate(Math.min(n, this.getDaysInMonth()));
          return this;
          };





          share|improve this answer























          • thank for answer, but I can´t add files to application :-(
            – chris
            Sep 26 '13 at 6:53






          • 1




            @chrisツ how about copying and pasting the source code of Datejs to your code. Just remember to include the copyright of the code.
            – OnesimusUnbound
            Sep 26 '13 at 7:01










          • LOL I was just working on doing that exact extraction to amend my answer.
            – cbeckner
            Sep 26 '13 at 7:08



















          6














          I also needed a list of the last 12 months this is what I did:



          var theMonths = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
          var today = new Date();
          var aMonth = today.getMonth();
          var i;
          for (i=0; i<12; i++) {
          document.writeln(theMonths[aMonth] + '<br>'); //here you can do whatever you want...
          aMonth++;
          if (aMonth > 11) {
          aMonth = 0;
          }
          }





          share|improve this answer

















          • 1




            cool, thanks for sharing!
            – chris
            Feb 25 '15 at 11:41



















          0














          Including year of month




          var monthName = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
          var d = new Date();
          for (i=0; i<=12; i++) {
          console.log(monthName[d.getMonth()] + ' ' + d.getFullYear());
          d.setMonth(d.getMonth() - 1);
          }








          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f19021117%2flast-12-months-in-javascript%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            3














            Use Datejs (http://www.datejs.com/)



            It has a built in function to add months:



            Date.today().addMonths(-6);


            UPDATE:
            Since you're not able to include external files, here are the relevant methods from within Datejs.



            /*
            * Copyright (c) 2006-2007, Coolite Inc. (http://www.coolite.com/). All rights reserved.
            * License: Licensed under The MIT License. See license.txt and http://www.datejs.com/license/.
            * Website: http://www.datejs.com/ or http://www.coolite.com/datejs/
            */

            Date.isLeapYear = function (year) {
            return (((year % 4 === 0) && (year % 100 !== 0)) || (year % 400 === 0));
            };

            Date.prototype.isLeapYear = function () {
            var y = this.getFullYear();
            return (((y % 4 === 0) && (y % 100 !== 0)) || (y % 400 === 0));
            };

            Date.getDaysInMonth = function (year, month) {
            return [31, (Date.isLeapYear(year) ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month];
            };

            Date.prototype.getDaysInMonth = function () {
            return Date.getDaysInMonth(this.getFullYear(), this.getMonth());
            };

            Date.prototype.addMonths = function (value) {
            var n = this.getDate();
            this.setDate(1);
            this.setMonth(this.getMonth() + value);
            this.setDate(Math.min(n, this.getDaysInMonth()));
            return this;
            };





            share|improve this answer























            • thank for answer, but I can´t add files to application :-(
              – chris
              Sep 26 '13 at 6:53






            • 1




              @chrisツ how about copying and pasting the source code of Datejs to your code. Just remember to include the copyright of the code.
              – OnesimusUnbound
              Sep 26 '13 at 7:01










            • LOL I was just working on doing that exact extraction to amend my answer.
              – cbeckner
              Sep 26 '13 at 7:08
















            3














            Use Datejs (http://www.datejs.com/)



            It has a built in function to add months:



            Date.today().addMonths(-6);


            UPDATE:
            Since you're not able to include external files, here are the relevant methods from within Datejs.



            /*
            * Copyright (c) 2006-2007, Coolite Inc. (http://www.coolite.com/). All rights reserved.
            * License: Licensed under The MIT License. See license.txt and http://www.datejs.com/license/.
            * Website: http://www.datejs.com/ or http://www.coolite.com/datejs/
            */

            Date.isLeapYear = function (year) {
            return (((year % 4 === 0) && (year % 100 !== 0)) || (year % 400 === 0));
            };

            Date.prototype.isLeapYear = function () {
            var y = this.getFullYear();
            return (((y % 4 === 0) && (y % 100 !== 0)) || (y % 400 === 0));
            };

            Date.getDaysInMonth = function (year, month) {
            return [31, (Date.isLeapYear(year) ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month];
            };

            Date.prototype.getDaysInMonth = function () {
            return Date.getDaysInMonth(this.getFullYear(), this.getMonth());
            };

            Date.prototype.addMonths = function (value) {
            var n = this.getDate();
            this.setDate(1);
            this.setMonth(this.getMonth() + value);
            this.setDate(Math.min(n, this.getDaysInMonth()));
            return this;
            };





            share|improve this answer























            • thank for answer, but I can´t add files to application :-(
              – chris
              Sep 26 '13 at 6:53






            • 1




              @chrisツ how about copying and pasting the source code of Datejs to your code. Just remember to include the copyright of the code.
              – OnesimusUnbound
              Sep 26 '13 at 7:01










            • LOL I was just working on doing that exact extraction to amend my answer.
              – cbeckner
              Sep 26 '13 at 7:08














            3












            3








            3






            Use Datejs (http://www.datejs.com/)



            It has a built in function to add months:



            Date.today().addMonths(-6);


            UPDATE:
            Since you're not able to include external files, here are the relevant methods from within Datejs.



            /*
            * Copyright (c) 2006-2007, Coolite Inc. (http://www.coolite.com/). All rights reserved.
            * License: Licensed under The MIT License. See license.txt and http://www.datejs.com/license/.
            * Website: http://www.datejs.com/ or http://www.coolite.com/datejs/
            */

            Date.isLeapYear = function (year) {
            return (((year % 4 === 0) && (year % 100 !== 0)) || (year % 400 === 0));
            };

            Date.prototype.isLeapYear = function () {
            var y = this.getFullYear();
            return (((y % 4 === 0) && (y % 100 !== 0)) || (y % 400 === 0));
            };

            Date.getDaysInMonth = function (year, month) {
            return [31, (Date.isLeapYear(year) ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month];
            };

            Date.prototype.getDaysInMonth = function () {
            return Date.getDaysInMonth(this.getFullYear(), this.getMonth());
            };

            Date.prototype.addMonths = function (value) {
            var n = this.getDate();
            this.setDate(1);
            this.setMonth(this.getMonth() + value);
            this.setDate(Math.min(n, this.getDaysInMonth()));
            return this;
            };





            share|improve this answer














            Use Datejs (http://www.datejs.com/)



            It has a built in function to add months:



            Date.today().addMonths(-6);


            UPDATE:
            Since you're not able to include external files, here are the relevant methods from within Datejs.



            /*
            * Copyright (c) 2006-2007, Coolite Inc. (http://www.coolite.com/). All rights reserved.
            * License: Licensed under The MIT License. See license.txt and http://www.datejs.com/license/.
            * Website: http://www.datejs.com/ or http://www.coolite.com/datejs/
            */

            Date.isLeapYear = function (year) {
            return (((year % 4 === 0) && (year % 100 !== 0)) || (year % 400 === 0));
            };

            Date.prototype.isLeapYear = function () {
            var y = this.getFullYear();
            return (((y % 4 === 0) && (y % 100 !== 0)) || (y % 400 === 0));
            };

            Date.getDaysInMonth = function (year, month) {
            return [31, (Date.isLeapYear(year) ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month];
            };

            Date.prototype.getDaysInMonth = function () {
            return Date.getDaysInMonth(this.getFullYear(), this.getMonth());
            };

            Date.prototype.addMonths = function (value) {
            var n = this.getDate();
            this.setDate(1);
            this.setMonth(this.getMonth() + value);
            this.setDate(Math.min(n, this.getDaysInMonth()));
            return this;
            };






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Sep 26 '13 at 7:27

























            answered Sep 26 '13 at 6:45









            cbeckner

            1,7171316




            1,7171316












            • thank for answer, but I can´t add files to application :-(
              – chris
              Sep 26 '13 at 6:53






            • 1




              @chrisツ how about copying and pasting the source code of Datejs to your code. Just remember to include the copyright of the code.
              – OnesimusUnbound
              Sep 26 '13 at 7:01










            • LOL I was just working on doing that exact extraction to amend my answer.
              – cbeckner
              Sep 26 '13 at 7:08


















            • thank for answer, but I can´t add files to application :-(
              – chris
              Sep 26 '13 at 6:53






            • 1




              @chrisツ how about copying and pasting the source code of Datejs to your code. Just remember to include the copyright of the code.
              – OnesimusUnbound
              Sep 26 '13 at 7:01










            • LOL I was just working on doing that exact extraction to amend my answer.
              – cbeckner
              Sep 26 '13 at 7:08
















            thank for answer, but I can´t add files to application :-(
            – chris
            Sep 26 '13 at 6:53




            thank for answer, but I can´t add files to application :-(
            – chris
            Sep 26 '13 at 6:53




            1




            1




            @chrisツ how about copying and pasting the source code of Datejs to your code. Just remember to include the copyright of the code.
            – OnesimusUnbound
            Sep 26 '13 at 7:01




            @chrisツ how about copying and pasting the source code of Datejs to your code. Just remember to include the copyright of the code.
            – OnesimusUnbound
            Sep 26 '13 at 7:01












            LOL I was just working on doing that exact extraction to amend my answer.
            – cbeckner
            Sep 26 '13 at 7:08




            LOL I was just working on doing that exact extraction to amend my answer.
            – cbeckner
            Sep 26 '13 at 7:08













            6














            I also needed a list of the last 12 months this is what I did:



            var theMonths = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
            var today = new Date();
            var aMonth = today.getMonth();
            var i;
            for (i=0; i<12; i++) {
            document.writeln(theMonths[aMonth] + '<br>'); //here you can do whatever you want...
            aMonth++;
            if (aMonth > 11) {
            aMonth = 0;
            }
            }





            share|improve this answer

















            • 1




              cool, thanks for sharing!
              – chris
              Feb 25 '15 at 11:41
















            6














            I also needed a list of the last 12 months this is what I did:



            var theMonths = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
            var today = new Date();
            var aMonth = today.getMonth();
            var i;
            for (i=0; i<12; i++) {
            document.writeln(theMonths[aMonth] + '<br>'); //here you can do whatever you want...
            aMonth++;
            if (aMonth > 11) {
            aMonth = 0;
            }
            }





            share|improve this answer

















            • 1




              cool, thanks for sharing!
              – chris
              Feb 25 '15 at 11:41














            6












            6








            6






            I also needed a list of the last 12 months this is what I did:



            var theMonths = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
            var today = new Date();
            var aMonth = today.getMonth();
            var i;
            for (i=0; i<12; i++) {
            document.writeln(theMonths[aMonth] + '<br>'); //here you can do whatever you want...
            aMonth++;
            if (aMonth > 11) {
            aMonth = 0;
            }
            }





            share|improve this answer












            I also needed a list of the last 12 months this is what I did:



            var theMonths = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
            var today = new Date();
            var aMonth = today.getMonth();
            var i;
            for (i=0; i<12; i++) {
            document.writeln(theMonths[aMonth] + '<br>'); //here you can do whatever you want...
            aMonth++;
            if (aMonth > 11) {
            aMonth = 0;
            }
            }






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Feb 25 '15 at 9:46









            elad silver

            4,18922747




            4,18922747








            • 1




              cool, thanks for sharing!
              – chris
              Feb 25 '15 at 11:41














            • 1




              cool, thanks for sharing!
              – chris
              Feb 25 '15 at 11:41








            1




            1




            cool, thanks for sharing!
            – chris
            Feb 25 '15 at 11:41




            cool, thanks for sharing!
            – chris
            Feb 25 '15 at 11:41











            0














            Including year of month




            var monthName = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
            var d = new Date();
            for (i=0; i<=12; i++) {
            console.log(monthName[d.getMonth()] + ' ' + d.getFullYear());
            d.setMonth(d.getMonth() - 1);
            }








            share|improve this answer




























              0














              Including year of month




              var monthName = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
              var d = new Date();
              for (i=0; i<=12; i++) {
              console.log(monthName[d.getMonth()] + ' ' + d.getFullYear());
              d.setMonth(d.getMonth() - 1);
              }








              share|improve this answer


























                0












                0








                0






                Including year of month




                var monthName = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
                var d = new Date();
                for (i=0; i<=12; i++) {
                console.log(monthName[d.getMonth()] + ' ' + d.getFullYear());
                d.setMonth(d.getMonth() - 1);
                }








                share|improve this answer














                Including year of month




                var monthName = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
                var d = new Date();
                for (i=0; i<=12; i++) {
                console.log(monthName[d.getMonth()] + ' ' + d.getFullYear());
                d.setMonth(d.getMonth() - 1);
                }








                var monthName = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
                var d = new Date();
                for (i=0; i<=12; i++) {
                console.log(monthName[d.getMonth()] + ' ' + d.getFullYear());
                d.setMonth(d.getMonth() - 1);
                }





                var monthName = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
                var d = new Date();
                for (i=0; i<=12; i++) {
                console.log(monthName[d.getMonth()] + ' ' + d.getFullYear());
                d.setMonth(d.getMonth() - 1);
                }






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 12 '18 at 15:12

























                answered Nov 12 '18 at 15:01









                jlizanab

                32614




                32614






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f19021117%2flast-12-months-in-javascript%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Florida Star v. B. J. F.

                    Danny Elfman

                    Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues