How to fix getMonth in JavaScript giving previous month?

Sometimes, we want to fix getMonth in JavaScript giving previous month.

In this article, we’ll look at how to fix getMonth in JavaScript giving previous month.

How to fix getMonth in JavaScript giving previous month?

To fix getMonth in JavaScript giving previous month, we add 1 to the month returned by getMonth.

For instance, we write

const m = d.getMonth() + 1;

to add 1 to the month returned by d.getMonth to get the human readable month number from date d.

This is because getMonth returns 0 for January, 1 for February, etc.

Conclusion

To fix getMonth in JavaScript giving previous month, we add 1 to the month returned by getMonth.