Why does the month argument range from 0 to 11 in JavaScript's Date constructor?

后端 未结 8 1146
生来不讨喜
生来不讨喜 2020-11-22 15:31

When initializing a new Date object in JavaScript using the below call, I found out that the month argument counts starting from zero.

new Date(         


        
8条回答
  •  执念已碎
    2020-11-22 16:10

    It might be a flaw, but it's also very handy when you want to represent the months or day of the week as a string you can just create an array like ['jan,'feb' ...etc][new Date().getMonth()] in stead of ['','jan',feb ...etc][new Date().getMonth()] or ['jan','feb' ...etc][new Date().getMonth()-1]

    days of the month are normaly not named so you won't be making arrays with names for those. In this case 1-31 is easier to handle, so you want have to subtract 1 every time...

提交回复
热议问题