Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I find last monday using momentjs?

I need to work with some logic that requires week begins on Monday (not Sunday).

There is a function start of week I can use:

moment().tz('America/Los_Angeles').startOf('week').add(1, 'day')

However if its Sunday, I need to use last week's date and add 1 to get monday.

I'm hoping there is an easier to just get "last monday" no matter what the date is.

like image 984
chovy Avatar asked Sep 05 '25 03:09

chovy


2 Answers

I found the answer (documented) but there is a isoWeek which will start the week on Monday instead of Sunday.

Start week on Sunday:

moment().startOf('week');

Start week on Monday:

moment().startOf('isoWeek');
like image 83
chovy Avatar answered Sep 07 '25 20:09

chovy


To find the last Monday just use

moment().isoWeekday(-6); // ISO day of the week with 1 being Monday and 7 being Sunday.

Or Locale Aware version

moment().weekday(-7); // when Monday is the first day of the week
like image 36
vorotech Avatar answered Sep 07 '25 19:09

vorotech