get next week start and end using jquery and moment js

前端 未结 4 1278
灰色年华
灰色年华 2020-12-08 02:47

I searched for this question and found there is a no answer on Stackoverflow.. So I decided to answer it...

This question helps if you need to get the start/end of n

4条回答
  •  轮回少年
    2020-12-08 03:21

    A little late to the party but here is the simplest way I've found to express starts/ends of weeks. The isoWeek argument starts weeks on Monday according to the ISO 8601, while week starts weeks depending on your locale (so probably either Sunday or Monday).

    This week:

    moment().startOf('isoWeek')
    moment().endOf('isoWeek')
    

    Next week:

    moment().add(1, 'weeks').startOf('isoWeek')
    moment().add(1, 'weeks').endOf('isoWeek')
    

    Last week:

    moment().subtract(1, 'weeks').startOf('isoWeek')
    moment().subtract(1, 'weeks').endOf('isoWeek')
    

    I like these constructions because they are incredibly readable. It's also easy to go back or forward any number of weeks by specifying how many weeks you want in subtract or add.

提交回复
热议问题