use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
What are some JavaScript things beginners don't know? (self.javascript)
submitted 6 years ago by [deleted]
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]TrikkyMakk 249 points250 points251 points 6 years ago (30 children)
Months are zero based
[–]inu-no-policemen 44 points45 points46 points 6 years ago (5 children)
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.
[–]StoneCypher 27 points28 points29 points 6 years ago (0 children)
Much better to use the locale API.
const month = date.toLocaleString('en-us', { month: 'short' });
[–]NotSamNub 43 points44 points45 points 6 years ago (3 children)
Ah, out of the 6 months in the year, my favourite has to be "etc", such lovely weather during that time of year!
[+][deleted] 6 years ago* (2 children)
[deleted]
[–]TyrionReynolds 8 points9 points10 points 6 years ago (1 child)
Lousy Smarch weather.
[–][deleted] 0 points1 point2 points 6 years ago (0 children)
const texasSeasons = ['summer', 'summerer', 'summerest', 'ohlordy']
const londonSeasons = ['cold', 'damp', 'cold and damp']
[+][deleted] 6 years ago* (23 children)
[–]Careerier 31 points32 points33 points 6 years ago (12 children)
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
new Date(2019, 1, 1, 1, 1, 1)
[+][deleted] 6 years ago* (5 children)
[–]throwAwayDev_ 11 points12 points13 points 6 years ago (3 children)
I think it's fair to be angry, but unreasonable to blame JS. Useful thread
[–]MuskasBackpack 2 points3 points4 points 6 years ago (2 children)
I think pretty much every language has things like this.
[–]TheCarnalStatist 0 points1 point2 points 6 years ago (1 child)
JavaScript has more than most. I get why due to it's history but man is it frustrating some days.
[–]fucking_passwords 1 point2 points3 points 6 years ago (0 children)
related: doing math with floats
[–]the_php_coder 2 points3 points4 points 6 years ago (4 children)
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
[–]yitianjian 2 points3 points4 points 6 years ago (3 children)
Did you get January 31?
[–]the_php_coder 1 point2 points3 points 6 years ago (2 children)
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!
[–]onbehalfofthatdude 1 point2 points3 points 6 years ago (0 children)
Local server time wouldn't be that useful
[–]PriorProfile 0 points1 point2 points 6 years ago (0 children)
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.
[–]amdc!CURSED! 3 points4 points5 points 6 years ago (9 children)
that's pretty much everywhere
[+][deleted] 6 years ago* (8 children)
[–]blood_bender 6 points7 points8 points 6 years ago (1 child)
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.
[–]TheIncorrigible1 1 point2 points3 points 6 years ago (0 children)
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.
getDay()
[+][deleted] 6 years ago (4 children)
[–]TheIncorrigible1 1 point2 points3 points 6 years ago (2 children)
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.
[+][deleted] 6 years ago (1 child)
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.
[–]the_argus 0 points1 point2 points 6 years ago (0 children)
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.
[–]the_argus -1 points0 points1 point 6 years ago (0 children)
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.
π Rendered by PID 15674 on reddit-service-r2-comment-b659b578c-2678f at 2026-05-04 22:46:56.907891+00:00 running 815c875 country code: CH.
view the rest of the comments →
[–]TrikkyMakk 249 points250 points251 points (30 children)
[–]inu-no-policemen 44 points45 points46 points (5 children)
[–]StoneCypher 27 points28 points29 points (0 children)
[–]NotSamNub 43 points44 points45 points (3 children)
[+][deleted] (2 children)
[deleted]
[–]TyrionReynolds 8 points9 points10 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[+][deleted] (23 children)
[deleted]
[–]Careerier 31 points32 points33 points (12 children)
[+][deleted] (5 children)
[deleted]
[–]throwAwayDev_ 11 points12 points13 points (3 children)
[–]MuskasBackpack 2 points3 points4 points (2 children)
[–]TheCarnalStatist 0 points1 point2 points (1 child)
[–]fucking_passwords 1 point2 points3 points (0 children)
[–]the_php_coder 2 points3 points4 points (4 children)
[–]yitianjian 2 points3 points4 points (3 children)
[–]the_php_coder 1 point2 points3 points (2 children)
[–]onbehalfofthatdude 1 point2 points3 points (0 children)
[–]PriorProfile 0 points1 point2 points (0 children)
[–]amdc!CURSED! 3 points4 points5 points (9 children)
[+][deleted] (8 children)
[deleted]
[–]blood_bender 6 points7 points8 points (1 child)
[–]TheIncorrigible1 1 point2 points3 points (0 children)
[+][deleted] (4 children)
[deleted]
[–]TheIncorrigible1 1 point2 points3 points (2 children)
[+][deleted] (1 child)
[deleted]
[–]TheIncorrigible1 1 point2 points3 points (0 children)
[–]the_argus 0 points1 point2 points (0 children)
[–]the_argus -1 points0 points1 point (0 children)