Sorry for the rudimentary question ...
I want to make an array from the date one month ago to the date two months later.
Also, I want to set the date to "date" and the day of the week to "week" as the property name.

Although it is a partial excerpt, I want to generate the following array for the final form.

obj [
    {date: '2020/11/20', week:'Friday'},
    {date: '2020/11/21', week:'Sat'},
    {date: '2020/11/22', week:'day'},
    {date: '2020/11/23', week:'Month'},
    {date: '2020/11/24', week:'Tue'},

As for what is currently being done, the following states have been achieved.
I just didn't know where to name the date date and add a week property.

obj {'2020/11/20', '2020/11/21', '2020/11/22',' 2020/11/23', '2020/11/24'}

Below is the code up to the above.

import dayjs from'dayjs';
dayjs.locale ('ja');
// Today's date
const today = dayjs ();
// Get the date one month ago from today
const beforeMonth = today.subtract (1,'months');
// Get the date 2 months ago from today
const afterMonth = today.add (2,'months');
// Get the date of the difference between 1 month ago and 2 months later
const totalNumber = afterMonth.diff (beforeMonth,'day');
const totalObject = new Object ();
for (var i = 0;i<totalNumber;i ++) {
    totalObject [i] = beforeMonth.add (i,'day');
    totalObject [i] = totalObject [i] .format ('YYYY/MM/DD');
console.log (totalObject);

I tried totalObject [i] .date etc. in the place of totalObject [i], but I got an error.
Below are the details of the error.

Uncaught TypeError: Cannot set property'date' of undefined

So, define it as totalObject.date =''? I tried it, but it didn't work.
I'm sorry for the rudimentary question, but I would appreciate it if you could answer!

  • Answer # 1

    I'm not sure what it is, but for the time being with js

    var d = new Date ();
    var start = d.setMonth (d.getMonth ()-1);
    var end = d.setMonth (d.getMonth () + 2);
    var count = (end-start)/1000/60/60/24 + 1;
    var dd = Array (count) .fill (null) .map ((x, y) =>(
      z = new Date (start + y * 60 * 60 * 24 * 1000),
      Object.fromEntries (
        ["date", z.toLocaleString (). Split (""). Splice (0) [0]],
        ["week", "Sun, Mon, Tue, Wed, Thu, Fri, Sat" .split ("") [z.getDay ()]],
    console.log (dd);