对于日程安排应用程序,我需要按星期一开始的星期几显示星期几。到目前为止,我到了:
var options = {
weekday: 'long',
year: 'numeric',
month: 'long',
day: 'numeric'
};
const weekDays = [];
for (let index = 1; index < 7; index++) {
weekDays.push(new Date(year, 0, index + (week - 1) * 7).toLocaleDateString('nl-NL', options));
}
此代码显示一周和一年给出的日期范围,但似乎无法使其在星期一开始。我有什么想法想念吗?
答案 0 :(得分:1)
您假设每年的1月1日是星期一。这段代码自动查找每年的第一个星期一。
var year = 2016;
var week = 1;
var index = 1;
var options = {
weekday: 'long',
year: 'numeric',
month: 'long',
day: 'numeric'
};
var d = new Date(year, 0, index + (week - 1) * 7);
var weekday = d.getDay();
var diff = 1 - weekday;
if (diff < 0) {
diff = diff + 7;
}
d.setDate(d.getDate() + diff);
console.log(d.toLocaleString(undefined, options));