Jump to content

Gettin The Next Month


jimfog

Recommended Posts

How am i going to use the getMonth method to get the next month from the user's pc. For example now is November, i want December to be displayed in the browser and when December comes january to be displayed(of the next year). How am i going to code the above using the date object?

Link to comment
Share on other sites

var d = new Date();var month = (d.getMonth() + 1) % 12;

That will give you the number of the month (ranging from 0 to 11). Then you can have an array for the months:

textMonths = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];alert(textMonths[month]);

Link to comment
Share on other sites

var d = new Date();var month = (d.getMonth() + 1) % 12;

That will give you the number of the month (ranging from 0 to 11). Then you can have an array for the months:

textMonths = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];alert(textMonths[month]);

why % 12? Isn't getting the index from getMonth (offset for the OP's specific request) sufficient? i.e.http://www.w3schools...ef_getmonth.asp edit: oh wait, is it to handle an out of bounds "exception" that would thrown if the month was December, in which case the next index would need to be 0, not 12?
Link to comment
Share on other sites

Have a look at the math. If it is December, getMonth will return 11. Add 1 you get 12. 12%12 is 0, which will get you January from the array. Just getting the index and adding one, will give you an error when it is December because the index 12 does not exist in the array.

Link to comment
Share on other sites

The code works ok, the next month is displayed, thanks. In the beginning i thought that the math was wrong. The truth is that i had mistaken the remainder% operator with the division operator/.

Link to comment
Share on other sites

The % operator much more flexible than testing for 12, because I could extend the function to get as many months in the future as I want and it will always be correct. For example:

function getFutureMonth(amount) {  return (getMonth() + amount) % 12;}

Link to comment
Share on other sites

Very useful for the app i am building, you answered in a potential future post i might made for the above.

Link to comment
Share on other sites

If All the months had the same number of days, the above solution would work fine, but currently if the date was 31st March, the month returned would be May, as it checks by the date as well, 31 days forward into 30 day April, returns May. you would have to check by a day below 28 to account for February, I just tend to set it to day 1

var f = new Date();var d = new Date(f.getFullYear(), f.getMonth(), 1);var month = (d.getMonth() + 1) % 12;

Link to comment
Share on other sites

And I thought the idea was simply to get the month that follows the first one, regardless of what day it was. Adding 1 to the current month always gets the next month.

Link to comment
Share on other sites

Adding 1 to the current month always gets the next month.
As long as you're checking against an external source, such as the array you used. If you actually set the date to the current month plus one, you could run into the issue dsonesuk brought up. However, I was under the same impression as you, that the OP was only concerned with getting the following month.
Link to comment
Share on other sites

Date() works fine for today, but it uses the date to roll over to the next 22 today, getmonth()+1, 22 in December, and so on. On January 31st when this date rolls over to February with no 31st, just up to 28th so it uses the days of the next month March 1,2,3, so you miss a month completely. try it

var d = new Date(2011, 1, 31);//var d = new Date();var month = (d.getMonth() + 1) % 12;

I've been caught out with this, everything works fine, Then when you in a month with more than the next, on the 31st everything goes crazy, unexpected months appear where they shouldn't, as will all months that are lower than 31.

Link to comment
Share on other sites

If you set the date, then yes it will roll over. But we are not setting the date, we are only getting the next month. On January 31 (or any other day of January) the getMonth function will return 0 correct? If you add 1 to that you get 1. Try this:

var d = new Date('01/31/2011');console.log(d.getMonth());console.log(d.getMonth()+1);

You'll see it outputs 0 and 1. That's all we're concerned with. This number is used as the index in the months array that Ingolme created in his code snippet:

var months = ['January', 'February', .... 'December'];

If we take the output from d.getMonth()+1 (ie, 1) and use that to access the month from the array (ie, months[d.getMonth()+1]) you will get "February".

Link to comment
Share on other sites

Date() works fine for today, but it uses the date to roll over to the next 22 today, getmonth()+1, 22 in December, and so on. On January 31st when this date rolls over to February with no 31st, just up to 28th so it uses the days of the next month March 1,2,3, so you miss a month completely. try it
var d = new Date(2011, 1, 31); //var d = new Date(); var month = (d.getMonth() + 1) % 12;

I've been caught out with this, everything works fine, Then when you in a month with more than the next, on the 31st everything goes crazy, unexpected months appear where they shouldn't, as will all months that are lower than 31.

If All the months had the same number of days, the above solution would work fine, but currently if the date was 31st March, the month returned would be May, as it checks by the date as well, 31 days forward into 30 day April, returns May. you would have to check by a day below 28 to account for February, I just tend to set it to day 1
var f = new Date(); var d = new Date(f.getFullYear(), f.getMonth(), 1); var month = (d.getMonth() + 1) % 12;

Sorry, but i am little confused. Why, when the month ends at the 31st of a month, the first version of the code will skip the next month and will go to the next one. I have not get it yet completely...sorry. For example March 31st, according to the array is month 2, why is it acknowledged by the system as month 3? Is the answer, that getMonth does not take into consideration only the month but also the days?
Link to comment
Share on other sites

If you set the date, then yes it will roll over. But we are not setting the date, we are only getting the next month. On January 31 (or any other day of January) the getMonth function will return 0 correct? If you add 1 to that you get 1. Try this:
var d = new Date('01/31/2011');console.log(d.getMonth());console.log(d.getMonth()+1);

You'll see it outputs 0 and 1. That's all we're concerned with. This number is used as the index in the months array that Ingolme created in his code snippet:

var months = ['January', 'February', .... 'December'];

If we take the output from d.getMonth()+1 (ie, 1) and use that to access the month from the array (ie, months[d.getMonth()+1]) you will get "February".

So,according to you shadowmage, the original version of the code works ok, there will not be any rollover problem.
Link to comment
Share on other sites

getMonth() returns an integer. Once we have the integer, we'll forget where it came from. It's just a number, for example, 10. If you add 1 to 10, you get 11, regardless of how many days the month actually has.

Link to comment
Share on other sites

The problem dsonesuk is referring to occurs when you set the date using the setMonth method. Try this:

var d = new Date('01/31/2011');d.setMonth(d.getMonth()+1);

The getMonth function returns 0, add 1 you get 1. The setMonth function sets the month to whatever value is provided (in this case 1). Since the getMonth/setMonth functions are 0-based the month corresponding to the index 1 is actually February. So the setMonth function is trying to set the date to Feb 31, 2011. February doesn't have 31 days so it rolls the left over days to March, giving you Mar 3, 2011 instead.

Link to comment
Share on other sites

Now i understand it. Then the (get nonth+1)%12 logic is just fine then. Programming...complex and complex...and complex, Some times i have the feeling i study math.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...