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

后端 未结 10 983
旧巷少年郎
旧巷少年郎 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:00

    get the next monday using moment

    moment().startOf('isoWeek').add(1, 'week');
    
    0 讨论(0)
  • 2020-12-02 16:08

    Most of these answers do not address the OP's question. Andrejs Kuzmins' is the best, but I would improve on it a little more so the algorithm accounts for locale.

    var nextMoOrTh = moment().isoWeekday([1,4,4,4,8,8,8][moment().isoWeekday()-1]);
    
    0 讨论(0)
  • 2020-12-02 16:08

    Here's e.g. next Monday:

    var chosenWeekday = 1 // Monday
    
    var nextChosenWeekday = chosenWeekday < moment().weekday() ? moment().weekday(chosenWeekday + 7) : moment().weekday(chosenWeekday)
    
    0 讨论(0)
  • 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'); 
    }
    
    0 讨论(0)
提交回复
热议问题