【发布时间】:2020-07-27 11:33:47
【问题描述】:
如何将给定的时间段划分为从该时间段开始开始的周列表?
例如,我有:
from: 2020-07-27T08:08:20.794Z,
to: 2020-09-24T08:08:20.794Z
星期必须是这样的:
27.07.2020 - 03.08.2020
04.08.2020 - 11.08.2020
12.08.2020 - 19.08.2020
20.08.2020 - 27.08.2020
28.08.2020 - 04.09.2020
05.09.2020 - 12.09.2020
13.09.2020 - 20.09.2020
21.09.2020 - 24.09.2020
我尝试使用此代码:
function getWeeksInMonth(month, year){
var weeks=[],
firstDate=new Date(year, month, 1),
lastDate=new Date(year, month+1, 0),
numDays= lastDate.getDate();
var start=1;
var end=7-firstDate.getDay();
while(start<=numDays){
weeks.push({start:start,end:end});
start = end + 1;
end = end + 7;
if(end>numDays)
end=numDays;
}
return weeks;
}
但它只返回我提供的month 和year。
【问题讨论】:
-
请访问help center,使用tour查看内容和How to Ask。做一些研究,搜索关于 SO 的相关主题;如果您遇到困难,请发布您的尝试minimal reproducible example,并使用
[<>]sn-p 编辑器记录输入和预期输出。
标签: javascript typescript datetime time