js 获取当前日期所在周
时间: 2023-08-30 16:07:47 浏览: 118
根据引用\[1\]中的代码,可以使用以下方法来获取当前日期所在周的日期列表:
```javascript
function getWeekDataList(data) {
var weekList = \[\];
var date = new Date(data);
// 判断本日期是否为周日,获取本周一日期
if (date.getDay() == "0") {
date.setDate(date.getDate() - 6);
} else {
date.setDate(date.getDate() - date.getDay() + 1);
}
// 获取本周一日期
var myDate = date.getDate();
var myMonth = date.getMonth() + 1;
if (date.getDate() < 10) {
myDate = '0' + myDate;
}
if (date.getMonth() + 1 < 10) {
myMonth = '0' + myMonth;
}
weekList.push(date.getFullYear() + "-" + myMonth + "-" + myDate);
// 获取周二以后日期
for (var i = 0; i < 6; i++) {
date.setDate(date.getDate() + 1);
myDate = date.getDate();
myMonth = date.getMonth() + 1;
if (date.getDate() < 10) {
myDate = '0' + myDate;
}
if (date.getMonth() + 1 < 10) {
myMonth = '0' + myMonth;
}
weekList.push(date.getFullYear() + "-" + myMonth + "-" + myDate);
}
return weekList;
}
```
调用该方法并传入当前日期,即可获取当前日期所在周的日期列表。
#### 引用[.reference_title]
- *1* [JS根据当前日期获取一周所有日期](https://blog.csdn.net/h__z__q/article/details/122110770)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item]
- *2* [【JS篇】获取当天所在的一周、前一周、下一周的日期和星期信息](https://blog.csdn.net/zxy602627878/article/details/125845272)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
阅读全文