new Date() set to 31 december 2014 says 1st december instead

JavascriptDateDatetime

Javascript Problem Overview


I am trying to convert a string to a Date object, and it works for all days except for December 31st where by object says December 1st instead of 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 appropriate value. Do you have any idea what I did wrong?

Javascript Solutions


Solution 1 - Javascript

The thing is, when you set a day first, you're still in the current month, so September. September has only 30 days so:

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 */

Solution 2 - Javascript

setMonth should before setDate: (not safe for Months less than 31 days)

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

And setMonth's second parameter also could be used to set date.

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


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

So, using setMonth and setDate separately would still cause unexpected result.

If the values set are greater than their logical range, the value will be auto adjusted to the adjacent value.

For example, if today is 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 using the constructor directly.

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

Solution 3 - Javascript

It's 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's wrapping it round.

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

Solution 4 - Javascript

Assuming your intent is to set year, month and date simultaneously you could use the longer date constructor:

> new Date(year, month, day, hour, minute, second, millisecond); > > [...] > > If at least two arguments are supplied, missing arguments are either > set to 1 (if day is missing) or 0 for all others.

So you would write:

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

As already established, setting one portion of date at a time can result in overflows:

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

Moreover, setting month before date can still cause unexpected overflow (answers that recommend changing the order of 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

Solution 5 - Javascript

The why of the behaviour and how to avoid it has been amply explained.

But the real error in your code is that you shouldn't use the default constructor: new Date(). Your code will result in a Date on Dec. 13 with the current time. I doubt this is what you want. You should use the Date constructor that takes year, month and day as parameters.

Solution 6 - Javascript

The answers made clear that the right order for setting the date is:

  • setFullYear()
  • setMonth()
  • setDate()

I just want to point out that it´s also important to set the year at first, because of the 29. february in leapyears.

Solution 7 - Javascript

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

Pass value as integer not string.. it will return u correct value..


Update - above description is not correct.. the main issue was you need to put these three line in proper sequence.. Even After I corrected the sequence I forgot to correct the description.. :P

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser2859409View Question on Stackoverflow
Solution 1 - JavascriptJakub KotrsView Answer on Stackoverflow
Solution 2 - JavascriptxdazzView Answer on Stackoverflow
Solution 3 - JavascriptfunkybroView Answer on Stackoverflow
Solution 4 - JavascriptSalman AView Answer on Stackoverflow
Solution 5 - JavascriptFlorian FView Answer on Stackoverflow
Solution 6 - JavascripthenmanView Answer on Stackoverflow
Solution 7 - JavascriptDeepak SharmaView Answer on Stackoverflow