Categories
JavaScript Answers

How to fix getMonth in JavaScript giving previous month?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *