Find next instance of a given weekday (ie. Monday) with moment.js

后端 未结 10 1061
旧巷少年郎
旧巷少年郎 2020-12-02 15:36

I want to get the date of the next Monday or Thursday (or today if it is Mon or Thurs). As Moment.js works within the bounds of a Sunday - Saturday, I\'m having to work out

10条回答
  •  抹茶落季
    2020-12-02 15:45

    Here's a solution to find the next Monday, or today if it is Monday:

    const dayOfWeek = moment().day('monday').hour(0).minute(0).second(0);
    
    const endOfToday = moment().hour(23).minute(59).second(59);
    
    if(dayOfWeek.isBefore(endOfToday)) {
      dayOfWeek.add(1, 'weeks');
    }
    

提交回复
热议问题