简体   繁体   English

如何获取下周的月、周、日格式列表?

[英]how to get month, week , day format list of next week?

i want to get the list of next week days in given format "March 12 Sunday" and then convert that is final list of week days as given below.我想以给定的格式“3 月 12 日星期日”获取下工作日的列表,然后将其转换为如下给出的最终工作日列表。 Here is a my current code with which i am trying to get desire output but that returns "06/12/22" format..这是我当前的代码,我试图用它来获得欲望 output 但返回“06/12/22”格式..

Current code:当前代码:

const nextWeek = [...Array(7).keys()].map(days => new Date(Date.now() + 86400000 * days).toLocaleDateString('en-us', { weekday:"long", month:"short", day:"numeric"}))
        console.log("== > ",nextWeek)

current output:当前 output:

["09/17/22", "09/18/22", "09/19/22", "09/20/22", "09/21/22", "09/22/22", "09/23/22"]

first i want this output首先我想要这个 output

 ["Sunday, March 4", "Monday, March 4", "Tuesday, March 4", "Wednesday, March 4", "Thursday, March 4", "Friday, March 4", "Saturday, March 4"]

and then final desire output is:然后最终的愿望 output 是:

const nextWeekdata = [

            { id: 1, name: "Sunday" ,date:21,Month:"March" },
            { id: 2, name: "Monday" ,date:22,Month:"March" },
            { id: 3, name: "Tuesday" ,date:23,Month:"March" },
            { id: 4, name: "Wednesday" ,date:24,Month:"March" },
            { id: 5, name: "Thursday" ,date:25,Month:"March" },
            { id: 6, name: "Friday" ,date:26,Month:"March" },
            { id: 7, name: "Saturday" ,date:27,Month:"March" },
          ];
    const nextWeek = [...Array(7).keys()].map((days) =>
  new Date(Date.now() + 86400000 * days).toLocaleDateString("en-us", {
    weekday: "long",
    month: "short",
    day: "numeric",
  })
);
let newArray = [];
nextWeek.forEach((item, index) => {
  let data = item.split(",");
  let secondLength = data[1].length;
  newArray.push({
    id: index + 1,
    name: data[0],
    date: data[1].substring(secondLength - 2),
    month: data[1].substring(1, secondLength - 3),
  });
});
console.log("days", newArray);

does this work?这行得通吗? yes是的

is this the right way to do it?这是正确的方法吗? nope

is this the easy way?这是简单的方法吗? for me, it is.对我来说,它是。

If you want to calculate the range of dates for next week, break your logic into smaller chunks.如果您想计算下周的日期范围,请将您的逻辑分解为更小的部分。

You should have three functions:您应该具有三个功能:

  • Function that gets the date for a given day of the week; Function 获取一周中给定日期的日期; next week下周
  • Function that gets the range of all the dates next week Function 获取下周所有日期的范围
  • Function to map the dates to object data Function 至 map 日期至 object 数据

 const getNextDay = (dayIndex) => { const today = new Date(); today.setDate(today.getDate() + (dayIndex - 1 - today.getDay() + 7) % 7 + 1); return today; }; const getNextWeek = () => [...Array(7).keys()].map(getNextDay); const nextWeek = getNextWeek().map((date, index) => ({ id: index + 1, name: date.toLocaleDateString('en-us', { weekday: 'long' }), date: date.getDate(), month: date.toLocaleDateString('en-us', { month: 'long' }), year: date.getFullYear() })); console.log(nextWeek);
 .as-console-wrapper { top: 0; max-height: 100%;important; }

Actually, when I use your current code I get this as a result:实际上,当我使用您当前的代码时,结果是:

(7) ['Saturday, Sep 17', 'Sunday, Sep 18', 'Monday, Sep 19', 'Tuesday, Sep 20', 'Wednesday, Sep 21', 'Thursday, Sep 22', 'Friday, Sep 23'] (7) ['Saturday, Sep 17', 'Sunday, Sep 18', 'Monday, Sep 19', 'Tuesday, Sep 20', 'Wednesday, Sep 21', 'Thursday, Sep 22', 'Friday, Sep 23']

Which means it works as you wish;这意味着它可以按您的意愿工作; what browser are you using?你使用的是什么浏览器?

Regarding the Second Question of yours, I'd have used something such as this:关于你的第二个问题,我会使用这样的东西:

class weekDay {
   constructer(id, x) {
      let name = x.split(", ")[0];
      let day = x.split(", ")[1].split(" ")[1];
      let month = x.split(", ")[1].split(" ")[0];
      this.id = id;
      this.name = name;
      this.day = day;
      this.month = month;
   }
}
const nextWeek = [...Array(7).keys()].map(days => new Date(Date.now() + 86400000 * days).toLocaleDateString('en-us', { weekday:"long", month:"long", day:"numeric"}))
var arr = [];
for (let i=0; i<=6; i++) {
   arr.push(new weekDay(i+1, nextWeek[i]));
}

This is my way of doing it, but take heed that I haven't tested it so it might have some syntax errors, if there was any, please notify me.这是我的做法,但请注意我没有测试它,所以它可能有一些语法错误,如果有的话,请通知我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM