Get last monday in month in moment.js

前端 未结 4 1500
梦毁少年i
梦毁少年i 2021-01-12 06:51

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\')

B

4条回答
  •  醉酒成梦
    2021-01-12 07:04

    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;
    

提交回复
热议问题