简体   繁体   English

如何获取本周的所有日期?

[英]How to get the ALL the dates of the current week?

Is there a way to get all the dates of the current week?有没有办法获取本周的所有日期? For example today is 11/22/2021.例如,今天是 2021 年 11 月 22 日。 So I need an array of ["11/21/2021", "11/22/20201", "11/23/2021", "11/24/2021", "11/25/2021", "11/26/2021", "11/27/2021"]所以我需要一个数组["11/21/2021", "11/22/20201", "11/23/2021", "11/24/2021", "11/25/2021", "11/26/2021", "11/27/2021"]

Also this array needs to be consistent for the entire week until it is Sunday, then it will create a new array.此外,这个数组需要在整个星期保持一致,直到星期天,然后它将创建一个新数组。

Something like that?类似的东西?

Array.from(Array(7).keys()).map((idx) => {const d = new Date(); d.setDate(d.getDate() - d.getDay() + idx); return d; });

Some explanation, Array(7) will create an empty array with 7 entries.一些解释, Array(7)将创建一个包含 7 个条目的空数组。

Array(7).keys() will return an iterator with the array keys. Array(7).keys()将返回一个带有数组键的迭代器。

Array.from(Array(7).keys()) will transform the iterator into a new array with 7 entries containing integers (aka [0, 1, 2, 3, 4, 5, 6] ) Array.from(Array(7).keys())将迭代器转换为一个新数组,其中包含 7 个包含整数的条目(又名[0, 1, 2, 3, 4, 5, 6]

d.getDate() - d.getDay() + idx , we take the current day (23), remove the day of week (0 for Sunday, 2 for Tuesday... Basically we calculate the last Sunday date) et add number of days to have every date in the week. d.getDate() - d.getDay() + idx ,我们取当前日期 (23),删除星期几(0 代表星期日,2 代表星期二...基本上我们计算最后一个星期日的日期)并添加数字一周中的每个日期的天数。

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

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