Categories
JavaScript Answers

How to get start and end of given month with moment.js and JavaScript?

Spread the love

Sometimes, we want to get start and end of given month with moment.js and JavaScript.

In this article, we’ll look at how to get start and end of given month with moment.js and JavaScript.

How to get start and end of given month with moment.js and JavaScript?

To get start and end of given month with moment.js and JavaScript, we call the startOf and endOf methods.

For instance, we write

const d1 = new moment().startOf("month").format("YYYY-DD-MM");
const d2 = new moment().endOf("month").format("YYYY-DD-MM");

to call startOf with 'month' to return a moment object with the start of the current month.

Likewise, we call startOf with 'month' to return a moment object with the end of the current month.

Then we call format to return a date string in YYYY-DD-MM format.

Conclusion

To get start and end of given month with moment.js and JavaScript, we call the startOf and endOf methods.

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 *