Update value every month using Javascript
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
add a comment |
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
add a comment |
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
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
javascript date frontend web-frontend
asked Nov 22 '18 at 19:13
Anna Polyakova turnhandupAnna Polyakova turnhandup
83
83
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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;
};
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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;
};
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
add a comment |
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;
};
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
add a comment |
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;
};
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;
};
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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