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.