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

在Ruby中获取从今日起未来7天的星期名称数组

Generate an Array of the Next 7 Days Starting from Today

Here's a straightforward, reliable way to create this array using JavaScript. It handles the week wrap-around automatically, so you don't have to worry about edge cases like starting on Saturday.

Step-by-Step Solution:

  1. First, define the full list of week days in the order your example uses (starting with Sunday):
const daysOfWeek = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
  1. Get today's position in this array. The Date.getDay() method returns 0 for Sunday, 1 for Monday, up to 6 for Saturday—this matches our array's index perfectly:
const todayIndex = new Date().getDay();
  1. Combine two slices of the daysOfWeek array to get the next 7 days:
    • The first slice takes days from today to the end of the week
    • The second slice takes days from the start of the week up to (but not including) today
const next7Days = [
  ...daysOfWeek.slice(todayIndex),
  ...daysOfWeek.slice(0, todayIndex)
];

Example Outputs:

  • If today is Sunday (todayIndex = 0):
    next7Days becomes ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]
  • If today is Saturday (todayIndex = 6):
    next7Days becomes ["Saturday","Sunday","Monday","Tuesday","Wednesday","Thursday","Friday"]

Quick Notes:

  • If you need the week to start with Monday instead, reorder the daysOfWeek array and adjust the index calculation (e.g., (todayIndex + 6) % 7 to map Monday to index 0).
  • For localized day names, replace the strings in daysOfWeek with translated versions, or use new Date().toLocaleDateString('your-language-code', { weekday: 'long' }) to fetch dynamic localized names.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.08 15:52:40