Reputation: 2187
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?
Upvotes: 37
Views: 32792
Reputation: 1462
According to Moment.js documentation, 'month' uses an array and the indices of an array are zero-based. Therefore, January is set to '0' and December is set to '11'.
For example, moment().month('November');
prints '10'.
Upvotes: 78
Reputation: 581
moment().month gets or sets the month and accepts numbers from 0 to 11. Key here is that index starts at 0, not 1. The below code will work as a solution for this question.
{Moment(yourDate).month(Moment(yourDate).month()-1).format("MMMM Do YYYY")}
The above code for 8/30/2022 will show August 30th 2022.
{Moment(yourDate).month(Moment(yourDate).month()-1).format("MM-DD-YYYY")}
The above code for 8/30/2022 will show 08-30-2022.
{Moment(yourDate).format("MM-DD-YYYY")}
The above code for 8/30/2022 will show 09-30-2022.
Upvotes: 1