Javascript Date: next month -


I am using the date of javascript for a project, but today I see that my code works first Now is not working correctly. Instead of producing the expected February, the code below produces march.

My code looks something like this:

  current = new date (); Current.setMonth (current.getMonth () + 1); // If today is January, then it is estimated to be by February  

This code works every day till date. Is this a JavaScript bug or am I going to go wrong about this?

You will probably find that you are deciding the date of February 31, 2009 (if today is January 31) and Javascript automatically rolls in the early part of March.

Check the day of the month, I hope it will be 1, 2 or 3. If it is not,

Thus, "the last day of January" day becomes "on the last day of February".

EDIT:

/ P>

Based on other comments in your comments, Ronald, you want to clarify the behavior of the side case, such as "when I "Or" When I try to make 2009/13/07 (yyyy / mm / dd) "(That last one can still be a problem for my solution, you should check it out) .

Instead, I will obviously have code for prospects since you do not care about the month of the month (you just want to correct the year and month for the next month), something like this Must be:

  var now = new date (); If (now. Gate) == 11) {var present = new date (now eleven years) + 1, 0, 1); } Else {var current = new date (now .get full yuan), now. GetMonth () + 1, 1); }  

The next day on January 1, in any day in December and any other day gives you on the first day of the next month. More codes, I know, but I have long been tired of coding for efficiency, prefer readability, unless there is no definite need to do so otherwise.


Comments