Javascript Month Index | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 5

Javascript Month Index

I'm so confuse with this: Date(2017, 0, 0).getDate() // 31 Date(2017,1,0).getDate() // 31 ???? Date(2017,2,0).getDate() // 28 ???? Someone please explain.

30th Aug 2017, 5:00 AM
Berli šŸ‡®šŸ‡©
Berli šŸ‡®šŸ‡© - avatar
3 Respostas
+ 5
From what I can see, the Date() constructor returns 31 when initialized with invalid arguments. However, if it can resolve the arguments for year and month, the getDate() method will return the number of days in that month. It also appears that the parameters are not using zero based indexes to specify month and day. Therefore, any lines specifying 0 for month will yield 31. The lines that specify 1 or 2 for month and 0 for days yields the number of days in January and February, respectively.
30th Aug 2017, 5:26 AM
David Carroll
David Carroll - avatar
+ 3
Thanks for that David. Sam, JS Month is 0-based, but thanks anyway :)
2nd Sep 2017, 11:50 AM
Berli šŸ‡®šŸ‡©
Berli šŸ‡®šŸ‡© - avatar
0
JavaScript oddly indexes months starting at 1. So the second option is January and the third is Feb
30th Aug 2017, 11:07 AM
S C