You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用INTL API获取从今日起未来一整周各天对应星期名称的问题

问题根因

你现有代码存在两个问题导致输出不符合预期:

  • 数组长度设置为1,仅能生成1个元素,无法覆盖7天的需求
  • 每次循环都重新获取当前日期new Date(),没有做天数偏移,所以所有返回值都是当天的星期

修复后的代码

function daysForLocale(localeName = 'en', weekday = 'long') {
  const format = new Intl.DateTimeFormat(localeName, { weekday }).format;
  const today = new Date();
  // 生成7个元素,对应未来1天到7天的偏移
  return [...Array(7).keys()]
    .map((offset) => {
      // 计算偏移后的日期
      const targetDate = new Date(today);
      targetDate.setDate(today.getDate() + offset + 1);
      return format(targetDate);
    });
}
console.log(daysForLocale());

补充说明

如果你需要的结果包含当天,只需要把offset + 1改成offset即可,输出会从当日星期开始按顺序排列。

内容的提问来源于stack exchange,提问作者user16698267

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.07 10:57:02