New date () set before December 31, 2014, instead of December 1

I am trying to convert a string to a Date object, and it works for all days except December 31, where on the object it says December 1 instead of the 31st. I have no idea why. Here is my JavaScript code:

 var dt = new Date(); dt.setDate("31"); dt.setMonth("11"); dt.setFullYear("2014"); 

but my variable value is:

 Mon Dec 01 2014 11:48:08 GMT+0100 (Paris, Madrid) 

If I do the same for any other date, my object returns to the corresponding value. Do you know what I did wrong?

+80
javascript date datetime
09 Sep '14 at 9:55
source share
7 answers

setMonth must before setDate : (unsafe for months less than 31 days)

 var dt = new Date(); dt.setFullYear(2014); dt.setMonth(11); dt.setDate(31); 

And setMonth second parameter can also be used to set the date.

 var dt = new Date(); dt.setFullYear(2014); dt.setMonth(11, 31); 




If there are no arguments for the constructor, it will use the current date and time. according to system settings.

Thus, using setMonth and setDate separately will still produce an unexpected result.

If the set values ​​are greater than their logical range , the value will automatically be set to an adjacent value .

For example, if today 2014-09-30 , then

 var dt = new Date(); dt.setFullYear(2014); /* Sep 30 2014 */ dt.setMonth(1); /* Mar 02 2014, see, here the auto adjustment occurs! */ dt.setDate(28); /* Mar 28 2014 */ 

To avoid this, set the values ​​directly using the constructor.

 var dt = new Date(2014, 11, 31); 
+83
Sep 09 '14 at 10:03
source share

The fact is that when you set the day first, you are still in the current month and in September. September has only 30 days, therefore:

 var dt = new Date(); /* today */ dt.setDate("31"); /* 1st Oct 2014 as should be by spec */ dt.setMonth("11"); /* 1st Dec 2014 */ dt.setFullYear("2014"); /* 1st Dec 2014 */ 
+118
Sep 09 '14 at 10:03
source share

This is because the first thing you do is

 dt.setDate(31) 

This sets the current date to 31. The current month is September, which has 30 days, so it completes it.

If you were to print the date after this point, it will say October 1st.

+23
Sep 09 '14 at 10:04
source share

Assuming that you intend to set the year, month, and date at the same time, you can use the constructor for a longer date:

new date (year, month, day, hour, minute, second, millisecond)

[...]

If at least two arguments are specified, the missing arguments are set to 1 (if the day is missing) or 0 for all others.

So you should write:

 var dt = new Date(2014, 11, 31); 

As already established, setting one part of the date at the same time can lead to overflow:

 var dt = new Date(2012, 1, 29); // Feb 29 2012 dt.setFullYear(2014); // Mar 01 2014 instead of Feb 28 2014 

In addition, setting the month to the date can still cause an unexpected overflow (answers recommending changing the order of the methods are incorrect):

 var dt = new Date(2014, 0, 31); // Jan 31 2014 dt.setFullYear(2014); // Jan 31 2014 dt.setMonth(1); // Mar 03 2014 instead of Feb 28 2014 dt.setDate(1); // Mar 01 2014 
+12
Sep 10 '14 at 6:16
source share

Why the behavior and how to avoid it is explained.

But the real mistake in your code is that you should not use the default constructor: new Date (). Your code will lead to the date December 13 with the current time. I doubt that this is what you want. You should use the Date constructor, which takes the year, month, and day as parameters.

+7
Sep 09 '14 at 22:04
source share

The answers revealed that the correct date setting order is:

  • setFullYear ()
  • setMonth ()
  • SETDATE ()

I just want to note that it is also important to set the year first, due February 29th at leapyears.

-one
Sep 11 '14 at 11:47
source share
 var dt = new Date(); dt.setFullYear(2014); dt.setMonth(11); dt.setDate(31); 

Pass the value as an integer, not a string .. it will return u the correct value.




Refresh - the description above is not correct .. The main problem was you need to put these three lines in the correct sequence .. Even after I corrected the sequence, I forgot to correct the description ..: P

-3
Sep 09 '14 at 10:02
source share



All Articles