I'm using Moment.js included through RequireJS. When I call Moment().month(), instead of number 11 I always get number 10. Any ideas how can this happen?
Asked
Active
Viewed 2.7k times
31
Ladislav M
- 2,107
- 4
- 33
- 52
-
8Isn't [january month 0](http://momentjs.com/docs/#/get-set/month/)? `Note: Months are zero indexed, so January is month 0.` – Ron van der Heijden Nov 20 '13 at 11:35
-
3in javascript's Date, months are counted 0-11. http://www.w3schools.com/jsref/jsref_obj_date.asp – foibs Nov 20 '13 at 11:36
-
3Try Moment().format("MM") – Renato Gama Nov 20 '13 at 11:37
-
You can always use the .add method to account for the zero index .add(1, 'M') – Jeremy Hamm Jul 21 '16 at 21:28
-
@JeremyHamm That doesn't work for December. `moment.parseZone('12/01/2018','MM/DD/YYYY').add({M:1}).month()` – Belden Apr 06 '18 at 16:26
1 Answers
67
According to the documentation, months are zero indexed. So January is 0 and December is 11. moment().month() would give the zero based index of the current month (November) which is 10.
Gnagy
- 1,342
- 15
- 26
-
14Just a clarification. 0-based indexing of the months is a feature that Moment.js inherits from Javascript. See the [ECMAScript specification](http://www.ecma-international.org/ecma-262/5.1/Ecma-262.pdf) at par.15.9.1.4 for further details. – Alberto De Caro Nov 20 '13 at 13:40
-
11Almost feels like they should expose it as 1-12 for the sake of intuitive use in lieu of the the ECMAScript specification. – Naruto Sempai Sep 26 '15 at 15:22
-
28by this logic why arent year, date, hours and minutes zero based?(snark, sorry) – Manuel Hernandez Apr 25 '16 at 05:17
-
2They actually claim month is not zero based: M MM 1..12 Month number – Moose on the Loose Jun 03 '19 at 04:17
-
working link to ECMAScript month spec: http://www.ecma-international.org/ecma-262/5.1/#sec-15.9.1.4 – haja Jan 28 '20 at 15:07
-
14We should create a dedicated web page that states the total dev time wasted by this. – Yaron Levi May 29 '20 at 16:40