Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get last monday in month in moment.js

Is there a way that I get the last monday in the month with moment.js?

I know I can get the end of the month with: moment().endOf('month')

But how about the last monday?

like image 347
dagda1 Avatar asked Aug 04 '14 20:08

dagda1


People also ask

How do you find the last day of the month in a moment?

We can use the startOf method to get the first day of the current month. And we can use the endOf method to get the last day of the current month.

How can I get a previous week in the moment?

I've used it this way : new Date(moment(). subtract(1, 'weeks').

How do you find the beginning of the month in a moment?

The moment(). month() method is used to get or set the month of the Moment object.


2 Answers

You're almost there. You just need to add a simple loop to step backward day-by-day until you find a Monday:

result = moment().endOf('month');
while (result.day() !== 1) {
    result.subtract(1, 'day');
}
return result;
like image 183
Andrew Miner Avatar answered Sep 22 '22 17:09

Andrew Miner


you get always monday with isoweek:

moment().endOf('month').startOf('isoWeek')
like image 22
vinjenzo Avatar answered Sep 23 '22 17:09

vinjenzo