r/javascript • • May 28 '19

What are some JavaScript things beginners don't know?

333 Upvotes

383 comments sorted by

View all comments

Show parent comments

0

u/[deleted] May 28 '19

[deleted]

7

u/blood_bender May 28 '19

I'm not positive it's an array underneath, but either way, months are special (or I guess days are special). First day of the month is 1, first month of the year is 0. It's not consistent which has always bothered me.

3

u/TheIncorrigible1 May 28 '19

It feels like someone designed months as if they were an enum and gave up on the rest of the Date API. Although, getDay() is also zero-based.

0

u/[deleted] May 28 '19

[deleted]

2

u/TheIncorrigible1 May 28 '19

I think the difference here is, a day doesn't represent an underlying structure. Both the day of the week and month of the year can be represented as enums and are both zero-based which supports this mentality.

0

u/[deleted] May 28 '19

[deleted]

2

u/TheIncorrigible1 May 28 '19

I agree. I've also made the same mistakes. If js had used some kind of interface/enum, it would be more clear, or was consistent with the other "Date" constructs such as day of the month being one-based. Unfortunately, it's way too late now.

1

u/the_argus May 28 '19

getDay() is zero based for same reason getMonth() is. So you can have an array of labels and use the result to get the correct index.

0

u/the_argus May 28 '19

It's not array based. The months come back zero based so you can have an array of month names and get the correct label. See also Date.prototype.getDay() for day of the week labels.