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.
Related
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)
I have a moment object that I want to subtract 1 day from. The original date shows as Sun Jul 15 2018 12:00:00 and I want to subtract 1 day from it so that it outputs as Sat Jul 14 2018 12:00:00.
This seems like it should be really easy if I use the subtract() function, but it's changing the date to the upcoming Saturday, not the Saturday before July 15. I'm assuming this has something to do with the week starting on July 15.
This seems to only be an issue when I'm using Sunday as my starting date. How can I make this work the way I need it to?
Here is my JS:
var timeFormat = 'dddd h:mma';
var originalDate = moment("sunday 12:00:00pm", timeFormat);
var previousDay = moment(originalDate).subtract(1, 'days').format(timeFormat);
var newDate = moment(previousDay+"12:00:00pm", timeFormat);
$(".openTime span").text(originalDate);
$(".newOpenTime span").text(newDate);
This outputs Sun Jul 15 2018 12:00:00 as the originalDate and Sat Jul 21 2018 12:00:00 as the date subtracted by 1 day. As you can see the new date is now Sat Jul 21 for some reason.
Here's a JSFiddle link: https://jsfiddle.net/dmcgrew/b5ev8knd/22/
The problem is that you use a formatted string to create the newDate. The string says something like this:
'Saturday 12:00:00pm'. MomentJs has no information what Saturday you actually mean, so it just takes the next one, which is the 21st of July.
If you just use the previousDay moment and format it, it works:
https://jsfiddle.net/b5ev8knd/36/
This question already has answers here:
Incorrect date shown in new Date() in JavaScript
(3 answers)
Closed 5 years ago.
When I convert that date to ISO string I get the 30th of november 2015. Why wouldn't it be first of december? I have googled and I know that month is 0-indexed and that overflows lead to the next day/month/year. But I cannot explain myself that behaviour and when I google it I find unrelated topics.
Thing is if You type:
var date = new Date(2015, 11, 1);
console.log(date);
You will get output based on your timezone, for me it's:
Tue Dec 01 2015 00:00:00 GMT+0100 (Central Europe Standard Time)
Function toISOString will always output time in UTC. So in this case you will get this date minus one hour.
2015-11-30T23:00:00.000Z
If you check the MDN page you'll see that:
Note: Where Date is called as a constructor with more than one argument, the specifed arguments represent local time. If UTC is desired, use new Date(Date.UTC(...)) with the same arguments.
Your users have different local timezones.
For me new Date(2015, 11, 1) gives Tue Dec 01 2015 00:00:00 GMT+0100 (Romance Standard Time) (I'm in the timezone Central European Time which is GMT+1).
Hence you can follow the MDN hint and use Date.UTC inside of your date call instead:
var date = new Date(2015, 11, 1);
console.log(date.toString());
// "Tue Dec 01 2015 00:00:00 GMT+0100 (Romance Standard Time)"
// 00:00:00 in GMT+1 but 23:00:00 in GMT+0
console.log(date.toISOString());
// "2015-11-30T23:00:00.000Z"
// ^^ 30th of november - that's a nogo!
var utcDate = new Date(Date.UTC(2015, 11, 1));
console.log(utcDate.toString());
// "Tue Dec 01 2015 01:00:00 GMT+0100 (Romance Standard Time) 15:49:26.146"
// 01:00:00 in GMT+1 but 00:00:00 in GMT+0
console.log(utcDate.toISOString());
// "2015-12-01T00:00:00.000Z"
// ^^ The first! Not the 30th!
var date = new Date(Date.UTC(2015, 11, 1));
console.log(date.toISOString());
Output:
2015-12-01T02:00:00.000Z
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
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)