生成包含从当前星期几开始的字符串数组的方法,使用 moment

huangapple go评论57阅读模式
英文:

How to generate array of string with the weekdays from the current weekday using moment

问题

想要生成包含星期几作为数组值的字符串动态数组,用于我正在使用的列表。

要求:=> 如果当前是星期一,则数组应为

['today','tomorrow','Wednesday','Thursday','Friday','Saturday','Sunday']

数组应根据当前日期动态生成。

英文:

Want to generate the dynamic array of string with the weekdays as a value inside the array for the list which I am using.

Required:=> If current weekday is Monday then array should be

['today','tomorrow','Wednesday','Thursday','Friday','Saturday','Sunday']

Array should be dynamic based on the current day.

答案1

得分: 0

以下是代码的翻译部分:

生成包含星期几的字符串的数组的代码如下

const daysArray = () => {
  const weekArray = [...Array(7 - moment().weekday())];
  const today = new Date();
  return weekArray.map((week, index) => {
    const dayName = moment(today)
      .add(index, "days")
      .calendar()
      .split(" at")[0];
    const date = moment(today).add(index, "days").format(DATE_FORMAT);

    return { value: dayName, name: dayName, date };
  }); 
};

这将生成包含动态星期几的对象数组。如果只返回 dayName,则会得到所期望的结果。

英文:

To generate array with the weekday as a string here is the code.

const daysArray = () => {
const weekArray = [...Array(7 - moment().weekday())];
const today = new Date();
return weekArray.map((week, index) => {
  const dayName = moment(today)
    .add(index, "days")
    .calendar()
    .split(" at")[0];
  const date = moment(today).add(index, "days").format(DATE_FORMAT);

  return { value: dayName, name: dayName, date };
}); 

};

This will generate the array of object with dynamic weekdays inside it. If you will return only dayName then you'll get you're desired result.

huangapple
  • 本文由 发表于 2023年2月8日 15:10:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75382413.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定