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

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?

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

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);

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.

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

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.

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.

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

Related

JavaScript newDate(Date.UTC) Results in the Wrong Month [duplicate]

using Mozilla Firefox Firebug:
var myDate = new Date(2012, 9, 23, 0,0,0,0);
myDate;
Date {Tue Oct 23 2012 00:00:00 GMT-0400 (Eastern Daylight Time)}
Why does javascript create the date with the wrong month?
No, javascript's Date months start with 0, so 9 is a 10th month and it is October
Reference:
new Date(year, month [, day, hour, minute, second, millisecond]);
[...]
month
Integer value representing the month, beginning with 0 for January to 11 for December.
In the javascript world months begin with zero!
kind of weird to me.
Anyhow, 9 is NOT September, but rather 9 is October.
Use a string as a parameter to avoid that weird behavior of Date constructor.
Example:
const myDate = new Date('2021-08-13'); // Result: Fri Aug 13 2021 02:00:00 GMT+0200...
In javascript Date object mounts are starting from ( 0 to 11 ) its funny :)
just always write
new Date(yea,month - 1,seconds ,millisecond)

Jquery => toUTCString() returns 1 day back date

I need to get format like below
19 Aug 2015 04:22:36 GMT
I have following code
var dt = '2015-08-19 04:22:36';
alert(new Date(dt).toUTCString().substr(4))
It returns me
18 Aug 2015 22:52:36 GMT
where as it should return
19 Aug 2015 22:52:36 GMT
What is wrong in my code
JsFiddle
As per answers below it seems it's converting the date to UTC date time.
I have date in UTC format in database. So please if some one could suggest the desired format without using toUTCString()
Update
Tried following
var dt = '2015-08-19 04:22:36 UTC';
alert(new Date(dt).toUTCString().substr(4))
It gives me liddate in FireFox and IE, chrome it is fine
Solved changed date string to
var dt = '2015/08/19 04:22:36 UTC';
Thanks
The toUTCString() converts your date to UTC so the outcome is correct!
Change your string to this var dt = '2015-08-19 04:22:36 UTC'; (notice the UTC)
or use the Date.UTC() function:
alert(new Date(Date.UTC(2015, 09, 19, 04, 22, 36)).toUTCString().substr(4))
Notice that the month is 0 based (0 -11) so to get August
you need to increase your monty by 1
From your profile, I can see you are 5 hours and 30 minutes ahead of UTC, so the var dt = '2015-08-19 04:22:36'; returns a Date object which is 5 hours and 30 minutes ahead of UTC time i.e. your local time. When you convert this date to UTC, it subtracts 5.5 hours and returns the Date object which is 22:56 previous night. The time part 04:22:36 is actually 4:22 a.m. or 4:22 a.m. at morning.

Javascript setUTCMonth does not work for November [duplicate]

This question already has answers here:
Javascript date parsing bug - fails for dates in June (??)
(5 answers)
Closed 8 years ago.
I'm getting some really strange behaviour with Javascript date objects.
var t = new Date();
t.setUTCFullYear(2014);
t.setUTCMonth(10);
t.setUTCDate(20);
console.log(t);
This returns:
Date {Sat Dec 20 2014 10:26:23 GMT-0800 (PST)}
Whereas if you use t.setMonth(10), you correctly get November. Am I doing something wrong?
This will work as expected tomorrow.
Today (October 31st), t is initialized with its day as 31. When you set the month to November, you're setting "November 31st", which is corrected to December 1st.
Then you set the date to 20, but the month has already changed. Set the date first, or better yet, use the two-argument verson of setUTCMonth():
t.setUTCMonth(10, 20);
Maybe I'm missing something (EDIT: I was, Paul's answer covers it), but you're right. However, this works:
var t = new Date();
t.setUTCFullYear(2014);
t.setUTCMonth(10, 20); // Set day here instead
console.log(t); // Thu Nov 20 2014 12:40:42 GMT-0600 (Central Standard Time)
It also works if you switch the order of the set statements:
var t = new Date();
t.setUTCFullYear(2014);
t.setUTCDate(20);
t.setUTCMonth(10);
The problem is a mixture of particular dates and the order of setting individual parts of the date. If you step through what's happening it becomes clearer:
New date is initialised as today: Oct 31, 2014
Setting the year to 2014: no effect
Setting the month to November: Nov 31, 2014 is interpreted as Dec 1, 2014
Setting the date to 20: Dec 20, 2014

JavaScript Date Bug February 2014

So I have this JS-code:
var d1 = new Date();
d1.setFullYear(2014);
d1.setMonth(1);
d1.setDate(1);
Should be Feb-01-2014, right? Only it's not... It returns Mar-01-2014 (actually, the full value is "Sat Mar 01 2014 20:54:29 GMT+0100 (Romance Standard Time)"). What the hell? Same thing happens with any other date value.
If I use this code, however, it works fine:
var d1 = new Date(2014, 1, 1, 0, 0, 0, 0);
The result is: Sat Feb 01 2014 00:00:00 GMT+0100 (Romance Standard Time)
Any ideas what's going on?
Here's what's happening, line for line:
You create a new date object with today's date.
var d1 = new Date(); // d1 = 2014-04-30
Then you set the year to 2014, which it already is, so nothing really happens.
d1.setFullYear(2014); // d1 = 2014-04-30
Here's the tricky part, because now you change the month to February. But this would make the date February the 30th (2014-02-30) which doesn't exist, so the JavaScript will try to find the closest valid date which is first of March (2014-03-01).
d1.setMonth(1); // d1 = 2014-02-30 is not valid so JS makes it 2014-03-01
Then you set the day to the first day of the month, which it already is, so nothing really happens here either.
d1.setDate(1) // d1 = 2014-03-01
You need to call setDate first. Basically it's grabbing the month and using the current date and since February doesn't have a 30th, it's defaulting to March.
Better to initialize Date, rather than have it default to the current date.
var d1 = new Date(0); // 1 January 1970 00:00:00 UTC
Try this:
d1.setFullYear(2014);
d1.setDate(1);
d1.setMonth(1);
What you were doing:
d1.setFullYear(2014); // change year to 2014 (30 Apr 2014 -> 30 Apr 2014)
d1.setMonth(1); // change month to 1 (30 Apr 2014 -> 30 Feb 2014, really 2 Mar 2014)
d1.setDate(1); // change day of month to 1 (2 Mar 2014 -> 1 Mar 2014)
By setting the date first, you're changing the date to 1 Apr 2014 before changing the month.

javascript is creating date wrong month

using Mozilla Firefox Firebug:
var myDate = new Date(2012, 9, 23, 0,0,0,0);
myDate;
Date {Tue Oct 23 2012 00:00:00 GMT-0400 (Eastern Daylight Time)}
Why does javascript create the date with the wrong month?
No, javascript's Date months start with 0, so 9 is a 10th month and it is October
Reference:
new Date(year, month [, day, hour, minute, second, millisecond]);
[...]
month
Integer value representing the month, beginning with 0 for January to 11 for December.
In the javascript world months begin with zero!
kind of weird to me.
Anyhow, 9 is NOT September, but rather 9 is October.
Use a string as a parameter to avoid that weird behavior of Date constructor.
Example:
const myDate = new Date('2021-08-13'); // Result: Fri Aug 13 2021 02:00:00 GMT+0200...
In javascript Date object mounts are starting from ( 0 to 11 ) its funny :)
just always write
new Date(yea,month - 1,seconds ,millisecond)

Categories

Resources