r/javascript • • May 28 '19

What are some JavaScript things beginners don't know?

333 Upvotes

383 comments sorted by

View all comments

247

u/TrikkyMakk May 28 '19

Months are zero based

47

u/inu-no-policemen May 28 '19

This allows you to use the month as an index:

> ['jan', 'feb', 'mar', 'apr', 'may', 'etc'][new Date().getMonth()]
"may"

It probably sounded like a good idea at the time.

Anyhow, that's why it is that way. This behavior isn't unique to JS either.

28

u/StoneCypher May 28 '19

Much better to use the locale API.

const month = date.toLocaleString('en-us', { month: 'short' });

44

u/NotSamNub May 28 '19

Ah, out of the 6 months in the year, my favourite has to be "etc", such lovely weather during that time of year!

0

u/[deleted] May 28 '19

[deleted]

10

u/TyrionReynolds May 28 '19

Lousy Smarch weather.

1

u/[deleted] May 29 '19

const texasSeasons = ['summer', 'summerer', 'summerest', 'ohlordy']

const londonSeasons = ['cold', 'damp', 'cold and damp']

0

u/[deleted] May 28 '19

[deleted]

34

u/Careerier May 28 '19

This is true, but that's something even beginners (hopefully) should know.

The date thing is weird because months start at zero, but days, hours, seconds, and milliseconds start at 1.

new Date(2019, 1, 1, 1, 1, 1) gives you Fri Feb 01 2019 01:01:01

0

u/[deleted] May 28 '19

[deleted]

12

u/throwAwayDev_ May 28 '19

I think it's fair to be angry, but unreasonable to blame JS. Useful thread

3

u/MuskasBackpack May 28 '19

I think pretty much every language has things like this.

→ More replies (2)
→ More replies (1)

3

u/the_php_coder May 28 '19

Not in the latest version of node LTS apparently. I just tried and I got jan instead of feb:

> new Date(2019, 1, 1, 1, 1, 1);
2019-01-31T19:31:01.000Z

3

u/yitianjian May 28 '19

Did you get January 31?

2

u/the_php_coder May 28 '19

Oh yeah and that too in UTC timezone, not my local one. Turns out this weird behavior is built into nodejs. Almost every other lang I've worked on gives local time by default, looks like I've got a lot to learn!

2

u/onbehalfofthatdude May 28 '19

Local server time wouldn't be that useful

→ More replies (1)

1

u/PriorProfile May 28 '19

This actually makes sense in a way.

Hours, minutes, seconds don’t start at 01:01:01. They start at 00:00:00.

Think of it this way: your example gives you the second millisecond of the second minute of the second hour of the first day of the second month.

Days are actually the odd ones. Not the months. But it would make even less sense to refer to days as 0 index I think.

5

u/amdc !CURSED! May 28 '19

that's pretty much everywhere

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.

→ More replies (1)
→ More replies (1)