0

what i'am missing here? Javascript does not know or skips 31.10.2022? (wich happens to be yesterday xD)

Here is a link to try it: https://playcode.io/1000164

thanks for your time and effort!

var thisDate = new Date('2022', '10','31');
console.log(thisDate.getDate() + '-' + thisDate.getMonth()); 

Prints:

1-11

Expected:

31-10

you can also do:

var X1 = 1; //- 29
var X2 = 30
var thisDate = new Date('2022', '10','1');
thisDate.setDate(thisDate.getDate() + X);
console.log(thisDate.getDate() + '-' + thisDate.getMonth()); 

If you take X1 (anything below 30) it works as expected. But if you add 30 (wich sums up to 31) ... its first of November ... why?

  • No, it's not yesterday. Months are zero-indexed, so `new Date('2022', '10','31');` is the 31st of *November*. Since the date doesn't exist, it rolls over to the 1st of December. – VLAZ Nov 01 '22 at 11:53
  • ah, that fixes it ... obviously i never had this occuring to me in the first place. – Kasi-Laubfrosch Nov 01 '22 at 12:05
  • Yeah it catches a few out, `days` are `1-31`, months are `0-11`,. I don't care if they were 0-based or 1 based, but the ECMA fudged that one up :). – Keith Nov 01 '22 at 12:24
  • @Keith you mean C because that's where it came up (or at least was popularised). JS followed suit as to be *more compatible and familiar* to programmers who already knew how to "properly" handle dates. And would have said that languages that didn't do it the same fudged it up. JS/ECMA is not alone in this, either. Even [Java did it](https://docs.oracle.com/javase/8/docs/api/java/util/Date.html#setMonth-int-). Well, and JS did actually follow the Java Date API from 1.0 more than it literally stuck to C but Java did get the idea from C anyway. – VLAZ Nov 04 '22 at 07:27

0 Answers0