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

后端 未结 10 1057
旧巷少年郎
旧巷少年郎 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 16:12

    The following can be used to get any next weekday date from now (or any date)

    var weekDayToFind = moment().day('Monday').weekday(); //change to searched day name
    
    var searchDate = moment(); //now or change to any date
    while (searchDate.weekday() !== weekDayToFind){ 
      searchDate.add(1, 'day'); 
    }
    

提交回复
热议问题