Hello I encountered strange behavior with javascript date. I show in this example:
var date = new Date(2017, 07, 22);
console.log(date); //22. 8. 2017
console.log(date.toLocaleDateString()) //Tue Aug 22 2017 00:00:00 GMT+0200
Why is month always increment? Is normal behavior or its my problem? Thanks
Javascript Date's month starts from 0. So 7 is actually 8th Month which is August.
month
Integer value representing the month, beginning with 0 for January to 11 for December.
In the JavaScript Date() object, the the month is an integer, starting at 0.
0 = January
1 = February
2 = March
and so on.
Javascript date month starts form zero index only ie jan is 0 and december is 11
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)
This question already has answers here:
`date.setMonth` causes the month to be set too high if `date` is at the end of the month
(8 answers)
Closed 3 years ago.
Given two Date objects, how to properly set the month of the first object to the month of another?
I'm facing a task of copying day, month and year from one Date object to another. Copying day and year works as intended, the problem comes up when I'm trying to copy the month.
Using b.setMonth(a.getMonth()) results in b having its month one too many.
Using b.setMonth(a.getMonth() - 1) however, results in b having its month one less than required.
The following typescript code:
let a = new Date(2018, 1, 12);
let b = new Date();
console.log(a);
console.log(b);
console.log('====');
console.log(a.getMonth());
console.log(b.getMonth());
b.setMonth(a.getMonth());
console.log('====');
console.log(a.getMonth());
console.log(b.getMonth());
b.setMonth(a.getMonth() - 1);
console.log('====');
console.log(a.getMonth());
console.log(b.getMonth());
Returns:
Mon Feb 12 2018 00:00:00 GMT+0100
Thu Aug 29 2019 16:11:03 GMT+0200
====
1
7
====
1
2
====
1
0 // 2 - 1 = 0 ?
Seemingly 2 - 1 should give 1 (a.getMonth() - 1), but apparently Date objects behave differently. What is the right way of copying month from one Date object to another in javascript/typescript? I suppose converting both dates to string, copying the right characters and parsing the string back to Date would work, but I'm wondering if there's an easier, more cleaner approach.
The problem is that the setMonth() method has an optional second parameter which is the day (DOCS). If you don't provide a value for the day it will automatically use the one of the date.
So, your A date is 12 February 2018 while your B date is 29 August 2019.
By doing b.setMonth(a.getMonth()); you are implicitly saying b.setMonth(1,29); (1 is a.getMonth() while 29 is the day form the b date).
So you are trying to set the date to the 29 February which isn't possible in 2019 and it shift the month by 1 to March (month 2).
If you do b.setMonth(a.getMonth() -1); you are setting it to the 29 January, which is possible so you get January as a month (month 1).
It's because it's your lucky (or unlucky!) day. It's the particular dates you're working woith.
February only has 28 days this year. When you set the month of "Aug 29 2019" to February, you're trying to create the invalid date "Feb 29 2019". That gets rounded up to "Mar 1 2019".
If you'd tried this experiment yesterday, you wouldn't have seen this problem.
This is a problem with the number of days in each month.
When you do:
b.setMonth(a.getMonth());
You are getting the b date but with february as the month: Thu Feb 29 2019 16:11:03 GMT+0200
And February of 2019 did not have 29 days. So the date is actually March 1st: Thu Mar 01 2019 16:11:03 GMT+0200
That is why you get month 2 in the second set of console logs.
And lastly you are setting b.month not a.month so it's subtracting one month from the a date (from February to January).
because you use a.getMonth() instead of b.getMonth().
So your main month is 1 and not 2.
while setting month you also need to specify the day you can use this code instead
var a = new Date(2018, 1, 12);
var b = new Date();
console.log(a);
console.log(b);
console.log('====');
console.log(a.getMonth());
console.log(b.getMonth());
b.setMonth(a.getMonth(),1);
console.log('====');
console.log(a.getMonth());
console.log(b.getMonth());
b.setMonth(a.getMonth() - 1,1);
console.log('====');
console.log(a.getMonth());
console.log(b.getMonth());
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)
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)
This question already has answers here:
Add 30 days to date (mm/dd/yy)
(4 answers)
Closed 4 years ago.
The operation date not working as expected when setting a next or previous month that no have the day that is in the actual date
>var date = new Date('2018/10/31');
undefined
>date
Wed Oct 31 2018 00:00:00 GMT-0600 (hora estándar central)
>date.setMonth(date.getMonth() + 1);
1543644000000
>date
Sat Dec 01 2018 00:00:00 GMT-0600 (hora estándar central)
Is this a bug and should it be reported or considered as the expected behavior?
It's documented behaviour. To quote from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setMonth for example:
If a parameter you specify is outside of the expected range, setMonth() attempts to update the date information in the Date object accordingly. For example, if you use 15 for monthValue, the year will be incremented by 1, and 3 will be used for month.
The current day of month will have an impact on the behaviour of this method. Conceptually it will add the number of days given by the current day of the month to the 1st day of the new month specified as the parameter, to return the new date. For example, if the current value is 31st August 2016, calling setMonth with a value of 1 will return 2nd March 2016. This is because in 2016 February had 29 days.
In your case, it's doing what's described above in the second paragraph: you've changed the month to November, but you haven't changed the day. And of course there's no 31st of November, so JavaScript automatically sets the date to the next valid date instead, which is 1st December.