使用 INTL 获取从今天开始的整周天数
Using INTL to get the full week's days from today
我正在尝试使用 INTL 从特定日期获取星期几(例如,今天是星期三,所以我想获取今天加上今天之后的 7 天)。我已经设法得到今天的日期,但我不确定如何得到剩下的日子。
function daysForLocale(localeName = 'en', weekday = 'long') {
const format = new Intl.DateTimeFormat(localeName, { weekday }).format;
return [...Array(1).keys()]
.map((day) => format(new Date()));
}
console.log(daysForLocale());
输出:
["Wednesday"]
["Wednesday"]
["Wednesday"]
["Wednesday"]
["Wednesday"]
["Wednesday"]
["Wednesday"]
期望的输出
["Thursday"]
["Friday"]
["Saturday"]
["Sunday"]
["Monday"]
["Tuesday"]
["Wednesday"]
请使用此代码。
function daysForLocale(localeName = 'en', weekday = 'long') {
const format = new Intl.DateTimeFormat(localeName, { weekday }).format;
const today = new Date();
return [...Array(7).keys()]
.map((val, index) => [format(new Date(today.getTime() + 3600 * 24 * 1000 * (index + 1)))]);
}
console.log(daysForLocale());
我正在尝试使用 INTL 从特定日期获取星期几(例如,今天是星期三,所以我想获取今天加上今天之后的 7 天)。我已经设法得到今天的日期,但我不确定如何得到剩下的日子。
function daysForLocale(localeName = 'en', weekday = 'long') {
const format = new Intl.DateTimeFormat(localeName, { weekday }).format;
return [...Array(1).keys()]
.map((day) => format(new Date()));
}
console.log(daysForLocale());
输出:
["Wednesday"]
["Wednesday"]
["Wednesday"]
["Wednesday"]
["Wednesday"]
["Wednesday"]
["Wednesday"]
期望的输出
["Thursday"]
["Friday"]
["Saturday"]
["Sunday"]
["Monday"]
["Tuesday"]
["Wednesday"]
请使用此代码。
function daysForLocale(localeName = 'en', weekday = 'long') {
const format = new Intl.DateTimeFormat(localeName, { weekday }).format;
const today = new Date();
return [...Array(7).keys()]
.map((val, index) => [format(new Date(today.getTime() + 3600 * 24 * 1000 * (index + 1)))]);
}
console.log(daysForLocale());