Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 441
user2859409 Avatar asked Sep 09 '14 09:09

user2859409


People also ask

What does new Date () return?

"The expression new Date() returns the current time in internal format, as an object containing the number of milliseconds elapsed since the start of 1970 in UTC.

What is new Date () in JavaScript?

The Date object is an inbuilt datatype of JavaScript language. It is used to work with dates and times. The Date object is created by using new keyword, i.e. new Date(). The Date object can be used date and time in terms of millisecond precision within 100 million days before or after 1/1/1970.

How do you add one day to a new Date?

const date = new Date(); date. setDate(date. getDate() + 1); // ✅ 1 Day added console.

How does JavaScript know the Date?

The date and time is broken up and printed in a way that we can understand as humans. JavaScript, however, understands the date based on a timestamp derived from Unix time, which is a value consisting of the number of milliseconds that have passed since midnight on January 1st, 1970.


2 Answers

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 */ 
like image 167
Jakub Michálek Avatar answered Oct 13 '22 10:10

Jakub Michálek


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); 
like image 31
xdazz Avatar answered Oct 13 '22 12:10

xdazz