Update value every month using Javascript












-1















everyone!
I was looking for solution of this for a long time and didn't find anything.



I need to make element on page which will have +=2000 every month from ..let's say today's date.
And I am puzzled without any idea how to do this.
I easily wrote updating value every 10 seconds, but what about months that have different length and so on?



Should I compare difference between current date and today's date, than do +=2000*numberOfMonths? Then how often should I check if month has passed no to kill speedload?



Or is there any other convinient way to do it?



I know the solution might be easy, but I don't get it.
Will be gratefull for any suggestions.










share|improve this question



























    -1















    everyone!
    I was looking for solution of this for a long time and didn't find anything.



    I need to make element on page which will have +=2000 every month from ..let's say today's date.
    And I am puzzled without any idea how to do this.
    I easily wrote updating value every 10 seconds, but what about months that have different length and so on?



    Should I compare difference between current date and today's date, than do +=2000*numberOfMonths? Then how often should I check if month has passed no to kill speedload?



    Or is there any other convinient way to do it?



    I know the solution might be easy, but I don't get it.
    Will be gratefull for any suggestions.










    share|improve this question

























      -1












      -1








      -1








      everyone!
      I was looking for solution of this for a long time and didn't find anything.



      I need to make element on page which will have +=2000 every month from ..let's say today's date.
      And I am puzzled without any idea how to do this.
      I easily wrote updating value every 10 seconds, but what about months that have different length and so on?



      Should I compare difference between current date and today's date, than do +=2000*numberOfMonths? Then how often should I check if month has passed no to kill speedload?



      Or is there any other convinient way to do it?



      I know the solution might be easy, but I don't get it.
      Will be gratefull for any suggestions.










      share|improve this question














      everyone!
      I was looking for solution of this for a long time and didn't find anything.



      I need to make element on page which will have +=2000 every month from ..let's say today's date.
      And I am puzzled without any idea how to do this.
      I easily wrote updating value every 10 seconds, but what about months that have different length and so on?



      Should I compare difference between current date and today's date, than do +=2000*numberOfMonths? Then how often should I check if month has passed no to kill speedload?



      Or is there any other convinient way to do it?



      I know the solution might be easy, but I don't get it.
      Will be gratefull for any suggestions.







      javascript date frontend web-frontend






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 22 '18 at 19:13









      Anna Polyakova turnhandupAnna Polyakova turnhandup

      83




      83
























          1 Answer
          1






          active

          oldest

          votes


















          1














          You can do it like:



            const getMonthsPassed = () => {
          const startDate = new Date(2018, 10, 22); // month start at 0
          const currentDate = new Date();
          const monthDifference =
          (currentDate.getFullYear() - startDate.getFullYear()) * 12 +
          (currentDate.getMonth() - startDate.getMonth());
          return monthDifference;
          };





          share|improve this answer
























          • Hmm, but what about how often should I check this and than update value? How to set for example "Let's check this const value every week, maybe the new month is here?"

            – Anna Polyakova turnhandup
            Nov 22 '18 at 19:23








          • 1





            I think i have misunderstood the requirement. You want to have a month counter element, and for it to update the number of months since today? On the web you would expect to only need to run this once on page load. Although I suppose you can run a timeout function every minute to check if it's absolutely essential

            – Cecil
            Nov 22 '18 at 19:27











          • Yeah, you're absolutely right. Sorry, I am just starting with JS) Thank you a lot for the answer, it's elegant. Marked as the right one!

            – Anna Polyakova turnhandup
            Nov 22 '18 at 19:40













          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%2f53436921%2fupdate-value-every-month-using-javascript%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          You can do it like:



            const getMonthsPassed = () => {
          const startDate = new Date(2018, 10, 22); // month start at 0
          const currentDate = new Date();
          const monthDifference =
          (currentDate.getFullYear() - startDate.getFullYear()) * 12 +
          (currentDate.getMonth() - startDate.getMonth());
          return monthDifference;
          };





          share|improve this answer
























          • Hmm, but what about how often should I check this and than update value? How to set for example "Let's check this const value every week, maybe the new month is here?"

            – Anna Polyakova turnhandup
            Nov 22 '18 at 19:23








          • 1





            I think i have misunderstood the requirement. You want to have a month counter element, and for it to update the number of months since today? On the web you would expect to only need to run this once on page load. Although I suppose you can run a timeout function every minute to check if it's absolutely essential

            – Cecil
            Nov 22 '18 at 19:27











          • Yeah, you're absolutely right. Sorry, I am just starting with JS) Thank you a lot for the answer, it's elegant. Marked as the right one!

            – Anna Polyakova turnhandup
            Nov 22 '18 at 19:40


















          1














          You can do it like:



            const getMonthsPassed = () => {
          const startDate = new Date(2018, 10, 22); // month start at 0
          const currentDate = new Date();
          const monthDifference =
          (currentDate.getFullYear() - startDate.getFullYear()) * 12 +
          (currentDate.getMonth() - startDate.getMonth());
          return monthDifference;
          };





          share|improve this answer
























          • Hmm, but what about how often should I check this and than update value? How to set for example "Let's check this const value every week, maybe the new month is here?"

            – Anna Polyakova turnhandup
            Nov 22 '18 at 19:23








          • 1





            I think i have misunderstood the requirement. You want to have a month counter element, and for it to update the number of months since today? On the web you would expect to only need to run this once on page load. Although I suppose you can run a timeout function every minute to check if it's absolutely essential

            – Cecil
            Nov 22 '18 at 19:27











          • Yeah, you're absolutely right. Sorry, I am just starting with JS) Thank you a lot for the answer, it's elegant. Marked as the right one!

            – Anna Polyakova turnhandup
            Nov 22 '18 at 19:40
















          1












          1








          1







          You can do it like:



            const getMonthsPassed = () => {
          const startDate = new Date(2018, 10, 22); // month start at 0
          const currentDate = new Date();
          const monthDifference =
          (currentDate.getFullYear() - startDate.getFullYear()) * 12 +
          (currentDate.getMonth() - startDate.getMonth());
          return monthDifference;
          };





          share|improve this answer













          You can do it like:



            const getMonthsPassed = () => {
          const startDate = new Date(2018, 10, 22); // month start at 0
          const currentDate = new Date();
          const monthDifference =
          (currentDate.getFullYear() - startDate.getFullYear()) * 12 +
          (currentDate.getMonth() - startDate.getMonth());
          return monthDifference;
          };






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 22 '18 at 19:22









          CecilCecil

          22625




          22625













          • Hmm, but what about how often should I check this and than update value? How to set for example "Let's check this const value every week, maybe the new month is here?"

            – Anna Polyakova turnhandup
            Nov 22 '18 at 19:23








          • 1





            I think i have misunderstood the requirement. You want to have a month counter element, and for it to update the number of months since today? On the web you would expect to only need to run this once on page load. Although I suppose you can run a timeout function every minute to check if it's absolutely essential

            – Cecil
            Nov 22 '18 at 19:27











          • Yeah, you're absolutely right. Sorry, I am just starting with JS) Thank you a lot for the answer, it's elegant. Marked as the right one!

            – Anna Polyakova turnhandup
            Nov 22 '18 at 19:40





















          • Hmm, but what about how often should I check this and than update value? How to set for example "Let's check this const value every week, maybe the new month is here?"

            – Anna Polyakova turnhandup
            Nov 22 '18 at 19:23








          • 1





            I think i have misunderstood the requirement. You want to have a month counter element, and for it to update the number of months since today? On the web you would expect to only need to run this once on page load. Although I suppose you can run a timeout function every minute to check if it's absolutely essential

            – Cecil
            Nov 22 '18 at 19:27











          • Yeah, you're absolutely right. Sorry, I am just starting with JS) Thank you a lot for the answer, it's elegant. Marked as the right one!

            – Anna Polyakova turnhandup
            Nov 22 '18 at 19:40



















          Hmm, but what about how often should I check this and than update value? How to set for example "Let's check this const value every week, maybe the new month is here?"

          – Anna Polyakova turnhandup
          Nov 22 '18 at 19:23







          Hmm, but what about how often should I check this and than update value? How to set for example "Let's check this const value every week, maybe the new month is here?"

          – Anna Polyakova turnhandup
          Nov 22 '18 at 19:23






          1




          1





          I think i have misunderstood the requirement. You want to have a month counter element, and for it to update the number of months since today? On the web you would expect to only need to run this once on page load. Although I suppose you can run a timeout function every minute to check if it's absolutely essential

          – Cecil
          Nov 22 '18 at 19:27





          I think i have misunderstood the requirement. You want to have a month counter element, and for it to update the number of months since today? On the web you would expect to only need to run this once on page load. Although I suppose you can run a timeout function every minute to check if it's absolutely essential

          – Cecil
          Nov 22 '18 at 19:27













          Yeah, you're absolutely right. Sorry, I am just starting with JS) Thank you a lot for the answer, it's elegant. Marked as the right one!

          – Anna Polyakova turnhandup
          Nov 22 '18 at 19:40







          Yeah, you're absolutely right. Sorry, I am just starting with JS) Thank you a lot for the answer, it's elegant. Marked as the right one!

          – Anna Polyakova turnhandup
          Nov 22 '18 at 19:40




















          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.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53436921%2fupdate-value-every-month-using-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

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          TypeError: fit_transform() missing 1 required positional argument: 'X'