Moment js get first and last day of current month
In case anyone missed the comments on the original question, you can use built-in methods (works as of Moment 1.7):
const startOfMonth = moment().startOf('month').format('YYYY-MM-DD hh:mm');
const endOfMonth = moment().endOf('month').format('YYYY-MM-DD hh:mm');
There would be another way to do this:
var begin = moment().format("YYYY-MM-01");
var end = moment().format("YYYY-MM-") + moment().daysInMonth();
You can do this without moment.js
A way to do this in native Javascript code :
var date = new Date(), y = date.getFullYear(), m = date.getMonth();
var firstDay = new Date(y, m, 1);
var lastDay = new Date(y, m + 1, 0);
firstDay = moment(firstDay).format(yourFormat);
lastDay = moment(lastDay).format(yourFormat);
moment startOf() and endOf() is the answer you are searching for.. For Example:-
moment().startOf('year'); // set to January 1st, 12:00 am this year
moment().startOf('month'); // set to the first of this month, 12:00 am
moment().startOf('week'); // set to the first day of this week, 12:00 am
moment().startOf('day'); // set to 12:00 am today