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

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

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

问题

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

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

  1. ['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

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

Array should be dynamic based on the current day.

答案1

得分: 0

以下是代码的翻译部分:

  1. 生成包含星期几的字符串的数组的代码如下
  2. const daysArray = () => {
  3. const weekArray = [...Array(7 - moment().weekday())];
  4. const today = new Date();
  5. return weekArray.map((week, index) => {
  6. const dayName = moment(today)
  7. .add(index, "days")
  8. .calendar()
  9. .split(" at")[0];
  10. const date = moment(today).add(index, "days").format(DATE_FORMAT);
  11. return { value: dayName, name: dayName, date };
  12. });
  13. };

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

英文:

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

  1. const daysArray = () => {
  2. const weekArray = [...Array(7 - moment().weekday())];
  3. const today = new Date();
  4. return weekArray.map((week, index) => {
  5. const dayName = moment(today)
  6. .add(index, "days")
  7. .calendar()
  8. .split(" at")[0];
  9. const date = moment(today).add(index, "days").format(DATE_FORMAT);
  10. return { value: dayName, name: dayName, date };
  11. });
  12. };

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:

确定