如何使用时刻迭代周数,start/end 周日期
How to iterate week number, start/end date of weeks using moment
如何获取特定月份的所有 week number
、from/to date
?
下面的代码是我构造来显示特定月份的 week number
、from/to date
每当我在循环中插入函数时,它 return 就是无限循环。
function getISOWeeksInMonth(month, year) {
let weekStart = new Date(year, month - 1, 1);
weekStart.setDate(weekStart.getDate() - (weekStart.getDay() || 7) + 1);
let weekEnd = new Date(weekStart);
weekEnd.setDate(weekEnd.getDate() + 6);
let weekNum = moment(weekStart, "YYYY-MM-DD").week()
let weeks = [];
do {
weeks.push({
weekNum : weekNum++,
start: new Date(weekStart),
end: new Date(weekEnd)
});
weekStart.setDate(weekStart.getDate() + 7);
weekEnd.setDate(weekEnd.getDate() + 7);
} while (weekStart.getMonth() < month);
return weeks;
}
_.forEach(moment.months(), function (month_name) {
var month_number = moment().month(month_name).format("MM");
getISOWeeksInMonth(month_number, 2022).forEach(week => console.log(
'Week : ' + week.weekNum +
'\nStart: ' + week.start.toDateString() +
'\nEnd : ' + week.end.toDateString())
);
})
结果应该return列表week number
,from/to date
示例 Url:
https://savvytime.com/week-number/philippines/2022
这是我 运行 函数外循环时的示例:
function getISOWeeksInMonth(month, year) {
let weekStart = new Date(year, month - 1, 1);
weekStart.setDate(weekStart.getDate() - (weekStart.getDay() || 7) + 1);
let weekEnd = new Date(weekStart);
weekEnd.setDate(weekEnd.getDate() + 6);
let weekNum = moment(weekStart, "YYYY-MM-DD").week()
let weeks = [];
do {
weeks.push({
weekNum : weekNum++,
start: new Date(weekStart),
end: new Date(weekEnd)
});
weekStart.setDate(weekStart.getDate() + 7);
weekEnd.setDate(weekEnd.getDate() + 7);
} while (weekStart.getMonth() < month);
return weeks;
}
getISOWeeksInMonth(1, 2022).forEach(week => console.log(
'Week : ' + week.weekNum +
'\nStart: ' + week.start.toDateString() +
'\nEnd : ' + week.end.toDateString())
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.2/moment.min.js"></script>
function getISOWeeksInMonth(month, year) {
let weekStart = new Date(year, month - 1, 1);
weekStart.setDate(weekStart.getDate() - (weekStart.getDay() || 7) + 1);
let weekEnd = new Date(weekStart);
weekEnd.setDate(weekEnd.getDate() + 6);
let weeks = [];
do {
let weekNum = moment(weekStart, "YYYY-MM-DD").week()
weeks.push({
weekNum : weekNum,
start: new Date(weekStart),
end: new Date(weekEnd)
});
weekStart.setDate(weekStart.getDate() + 7);
weekEnd.setDate(weekEnd.getDate() + 7);
} while (weekStart.getMonth() < month && (weekStart.getMonth() || (month < 12) ));
return weeks;
}
let _ = moment.months()
_.forEach(function (month_name) {
var month_number = moment().month(month_name).format("MM");
getISOWeeksInMonth(month_number, 2022).forEach(week => console.log(
'Week : ' + week.weekNum +
'\nStart: ' + week.start.toDateString() +
'\nEnd : ' + week.end.toDateString())
);
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.2/moment.min.js"></script>
当您计算 12 月(第 12 个月)的周数时,您从前一个月(11 月,即 11 日)的最后一周开始,并且在 do {...} while
循环中,您要添加7 天到 weekStart
。到目前为止一切顺利,但是您的 while (weekStart.getMonth() < month)
正在检查 weekStart.getMonth()
的 0-11 值,当它从 11 向前滚动时,它不会转到 12,而是转到 0。所以它总是小于12(十二月!)!
您可以通过将 while
调整为:
来解决此问题
while (weekStart.getMonth() < month && (weekStart.getMonth() || (month < 12) ))
如何获取特定月份的所有 week number
、from/to date
?
下面的代码是我构造来显示特定月份的 week number
、from/to date
每当我在循环中插入函数时,它 return 就是无限循环。
function getISOWeeksInMonth(month, year) {
let weekStart = new Date(year, month - 1, 1);
weekStart.setDate(weekStart.getDate() - (weekStart.getDay() || 7) + 1);
let weekEnd = new Date(weekStart);
weekEnd.setDate(weekEnd.getDate() + 6);
let weekNum = moment(weekStart, "YYYY-MM-DD").week()
let weeks = [];
do {
weeks.push({
weekNum : weekNum++,
start: new Date(weekStart),
end: new Date(weekEnd)
});
weekStart.setDate(weekStart.getDate() + 7);
weekEnd.setDate(weekEnd.getDate() + 7);
} while (weekStart.getMonth() < month);
return weeks;
}
_.forEach(moment.months(), function (month_name) {
var month_number = moment().month(month_name).format("MM");
getISOWeeksInMonth(month_number, 2022).forEach(week => console.log(
'Week : ' + week.weekNum +
'\nStart: ' + week.start.toDateString() +
'\nEnd : ' + week.end.toDateString())
);
})
结果应该return列表week number
,from/to date
示例 Url:
https://savvytime.com/week-number/philippines/2022
这是我 运行 函数外循环时的示例:
function getISOWeeksInMonth(month, year) {
let weekStart = new Date(year, month - 1, 1);
weekStart.setDate(weekStart.getDate() - (weekStart.getDay() || 7) + 1);
let weekEnd = new Date(weekStart);
weekEnd.setDate(weekEnd.getDate() + 6);
let weekNum = moment(weekStart, "YYYY-MM-DD").week()
let weeks = [];
do {
weeks.push({
weekNum : weekNum++,
start: new Date(weekStart),
end: new Date(weekEnd)
});
weekStart.setDate(weekStart.getDate() + 7);
weekEnd.setDate(weekEnd.getDate() + 7);
} while (weekStart.getMonth() < month);
return weeks;
}
getISOWeeksInMonth(1, 2022).forEach(week => console.log(
'Week : ' + week.weekNum +
'\nStart: ' + week.start.toDateString() +
'\nEnd : ' + week.end.toDateString())
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.2/moment.min.js"></script>
function getISOWeeksInMonth(month, year) {
let weekStart = new Date(year, month - 1, 1);
weekStart.setDate(weekStart.getDate() - (weekStart.getDay() || 7) + 1);
let weekEnd = new Date(weekStart);
weekEnd.setDate(weekEnd.getDate() + 6);
let weeks = [];
do {
let weekNum = moment(weekStart, "YYYY-MM-DD").week()
weeks.push({
weekNum : weekNum,
start: new Date(weekStart),
end: new Date(weekEnd)
});
weekStart.setDate(weekStart.getDate() + 7);
weekEnd.setDate(weekEnd.getDate() + 7);
} while (weekStart.getMonth() < month && (weekStart.getMonth() || (month < 12) ));
return weeks;
}
let _ = moment.months()
_.forEach(function (month_name) {
var month_number = moment().month(month_name).format("MM");
getISOWeeksInMonth(month_number, 2022).forEach(week => console.log(
'Week : ' + week.weekNum +
'\nStart: ' + week.start.toDateString() +
'\nEnd : ' + week.end.toDateString())
);
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.2/moment.min.js"></script>
当您计算 12 月(第 12 个月)的周数时,您从前一个月(11 月,即 11 日)的最后一周开始,并且在 do {...} while
循环中,您要添加7 天到 weekStart
。到目前为止一切顺利,但是您的 while (weekStart.getMonth() < month)
正在检查 weekStart.getMonth()
的 0-11 值,当它从 11 向前滚动时,它不会转到 12,而是转到 0。所以它总是小于12(十二月!)!
您可以通过将 while
调整为:
while (weekStart.getMonth() < month && (weekStart.getMonth() || (month < 12) ))