如何在 JavaScript 中查找星期几
How to find the days of the week in JavaScript
我正在尝试查找从今天到星期日的工作日。由于今天是星期一,我想显示从星期一到星期日的日期,但明天,我希望我的程序在星期二到星期日运行。
dateSets() {
let firstDayOfWeek = "";
let dates = [];
firstDayOfWeek = new Date();
let sunday = new Date();
sunday.setDate(sunday.getDate() - sunday.getDay() + 7);
const diff = sunday.getDate() - firstDayOfWeek.getDate();
dates.push(new Date());
for (let i = 0; i < diff; i++) {
dates.push(
new Date(firstDayOfWeek.setDate(firstDayOfWeek.getDate() + 1))
);
}
return dates;
},
这是查找星期几的另一个函数:
getDateOfWeek(week, year) {
let simple = new Date(year, 0, 1 + (week - 1) * 7);
let dow = simple.getDay();
let weekStart = simple;
if (dow <= 4) weekStart.setDate(simple.getDate() - simple.getDay() + 1);
else weekStart.setDate(simple.getDate() + 8 - simple.getDay());
return weekStart;
},
但是它没有像我预期的那样工作,在数据集中,只显示星期一而不显示其他日期,我不明白原因。如果你能帮我解决这个问题,我会很高兴。
谢谢...
这是您的选择,但如果您安装 NodeJS 并在 cmd 中打开您的文件夹并输入 'npm init' 和 'npm I days' 并打开您的编辑器并输入
var days = require('days');
console.log(days); // ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
控制台将显示一周中的所有日子,如果您想要特定的一天,您可以执行以下操作
var days = require('days');
console.log(days[0]); //Sunday
如果您在安装 NodeJS 方面需要帮助,请观看 YouTube 视频或回复此评论,我会提供帮助
let firstDayOfWeek = "";
let dates = [];
firstDayOfWeek = new Date();
let sunday = new Date();
sunday.setDate(sunday.getDate() - sunday.getDay() + 7);
//const diff = sunday.getDate() - firstDayOfWeek.getDate();
//Try this code
var timeDiff = Math.abs(sunday.getTime() - firstDayOfWeek.getTime());
var diff = Math.ceil(timeDiff / (1000 * 3600 * 24));
dates.push(new Date());
for (let i = 0; i < diff; i++) {
dates.push(
new Date(firstDayOfWeek.setDate(firstDayOfWeek.getDate() + 1))
);
}
return dates;
function getWeekDates(){
const dates = [new Date()]; // today
const curr = new Date();
const remDaysCount = 7-curr.getDay();
for(let i=1; i<= remDaysCount; i++){
// increase current Date by 1 and set to current Date
const nextDate = curr.setDate(curr.getDate()+1);
dates.push(new Date(nextDate));
}
return dates;
}
console.log(getWeekDates());
您的问题在这里:
const diff = sunday.getDate() - firstDayOfWeek.getDate()
当前日期是 9 月 27 日,下周日是 10 月 3 日,所以 diff 是 -4,for 循环测试 i < diff
从一开始就是错误的。考虑使用循环并将日期从今天递增到星期日。
function getDaysToSunday(date = new Date()) {
let d = new Date(+date);
let result = [];
do {
result.push(new Date(+d));
d.setDate(d.getDate() + 1);
} while (d.getDay() != 1)
return result;
}
console.log(getDaysToSunday().map(d=>d.toDateString()));
我正在尝试查找从今天到星期日的工作日。由于今天是星期一,我想显示从星期一到星期日的日期,但明天,我希望我的程序在星期二到星期日运行。
dateSets() {
let firstDayOfWeek = "";
let dates = [];
firstDayOfWeek = new Date();
let sunday = new Date();
sunday.setDate(sunday.getDate() - sunday.getDay() + 7);
const diff = sunday.getDate() - firstDayOfWeek.getDate();
dates.push(new Date());
for (let i = 0; i < diff; i++) {
dates.push(
new Date(firstDayOfWeek.setDate(firstDayOfWeek.getDate() + 1))
);
}
return dates;
},
这是查找星期几的另一个函数:
getDateOfWeek(week, year) {
let simple = new Date(year, 0, 1 + (week - 1) * 7);
let dow = simple.getDay();
let weekStart = simple;
if (dow <= 4) weekStart.setDate(simple.getDate() - simple.getDay() + 1);
else weekStart.setDate(simple.getDate() + 8 - simple.getDay());
return weekStart;
},
但是它没有像我预期的那样工作,在数据集中,只显示星期一而不显示其他日期,我不明白原因。如果你能帮我解决这个问题,我会很高兴。 谢谢...
这是您的选择,但如果您安装 NodeJS 并在 cmd 中打开您的文件夹并输入 'npm init' 和 'npm I days' 并打开您的编辑器并输入
var days = require('days');
console.log(days); // ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
控制台将显示一周中的所有日子,如果您想要特定的一天,您可以执行以下操作
var days = require('days');
console.log(days[0]); //Sunday
如果您在安装 NodeJS 方面需要帮助,请观看 YouTube 视频或回复此评论,我会提供帮助
let firstDayOfWeek = "";
let dates = [];
firstDayOfWeek = new Date();
let sunday = new Date();
sunday.setDate(sunday.getDate() - sunday.getDay() + 7);
//const diff = sunday.getDate() - firstDayOfWeek.getDate();
//Try this code
var timeDiff = Math.abs(sunday.getTime() - firstDayOfWeek.getTime());
var diff = Math.ceil(timeDiff / (1000 * 3600 * 24));
dates.push(new Date());
for (let i = 0; i < diff; i++) {
dates.push(
new Date(firstDayOfWeek.setDate(firstDayOfWeek.getDate() + 1))
);
}
return dates;
function getWeekDates(){
const dates = [new Date()]; // today
const curr = new Date();
const remDaysCount = 7-curr.getDay();
for(let i=1; i<= remDaysCount; i++){
// increase current Date by 1 and set to current Date
const nextDate = curr.setDate(curr.getDate()+1);
dates.push(new Date(nextDate));
}
return dates;
}
console.log(getWeekDates());
您的问题在这里:
const diff = sunday.getDate() - firstDayOfWeek.getDate()
当前日期是 9 月 27 日,下周日是 10 月 3 日,所以 diff 是 -4,for 循环测试 i < diff
从一开始就是错误的。考虑使用循环并将日期从今天递增到星期日。
function getDaysToSunday(date = new Date()) {
let d = new Date(+date);
let result = [];
do {
result.push(new Date(+d));
d.setDate(d.getDate() + 1);
} while (d.getDay() != 1)
return result;
}
console.log(getDaysToSunday().map(d=>d.toDateString()));