简体   繁体   English

在 javascript / moment.js 中获取每周的第一天

[英]Getting the first day of each week in javascript / moment.js

Is there a way to get all the dates in the weeks or just the first day of each week and the last day of each week?有没有办法获得几周内的所有日期,或者只是每周的第一天和每周的最后一天?

For example:例如:

date = 2020-04-15日期 = 2020-04-15

Then a code will get the date and take all the starting day of the week and the end day.然后代码将获取日期并获取一周的所有开始日期和结束日期。 The expected output is:预期的 output 为:

Week 1: 2020-04-06 to 2020-04-12第 1 周:2020-04-06 至 2020-04-12

Week 2: 2020-04-13 to 2020-04-19第 2 周:2020 年 4 月 13 日至 2020 年 4 月 19 日

Week 3: 2020-04-20 to 2020-04-26第 3 周:2020 年 4 月 20 日至 2020 年 4 月 26 日

Week 4: 2020-04-27 to 2020-05-03第 4 周:2020-04-27 至 2020-05-03

Get the first day of the current week (Sunday):获取当前一周的第一天(星期日):

moment().subtract(moment().weekday(), 'days');

Get the last day of the current week (Satursday):获取当前一周的最后一天(星期六):

moment().add(6 - moment().weekday(), 'days');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM