Getting the date of next Monday

前端 未结 9 502
臣服心动
臣服心动 2020-12-02 23:06

How can I get the next Monday in JavaScript? I can\'t find anything of this in the internet and I have also tried a lot of codes and understanding of this but I can\'t reall

9条回答
  •  半阙折子戏
    2020-12-02 23:31

    If you need to handle time zones, one option would be to use the UTCDate methods setUTCDate() and getUTCDate(). This allows for consistency so the calculation is the same no matter the time zone setting.

    var d = new Date();
    d.setUTCDate(d.getUTCDate() + (7 - d.getUTCDay()) % 7 + 1);
    

    (This is the example from above where on a Monday the following Monday is returned)

提交回复
热议问题