I needed to convert the date to a
new Date(year, monthIndex [, day [, hours [, minutes [, seconds [, milliseconds]]]]])
Great, the last form looks perfect. Off I went with this syntax:
const internationalWomensDay = new Date(2021, 3, 8)
Hmm, except the result was not as expected 🤔
//'Thu Apr 08 2021 00:00:00 GMT+0100 (British Summer Time)'
Why was it 8th April 2021 when I specifically passed in the value of 3 for March? It turned out that the parameter’s name monthIndex signifies something really important:
Integer value representing the month, beginning with 0 for January to 11 for December.
So the correct syntax for constructing the date
8th March 2021 is:
const internationalWomensDay = new Date(2021, 2, 8)
I am at loss to why it’s
monthIndex and not
month, and it’s base 0 and not base 1 😕. Especially so when
day both use base 1! It took me a couple of hours of debugging to recognise this.
Have you fallen into this trap before?