2

I have a small snippet of JS code that I've been using to set dates on the header of an HTML table based on an object, like so:

dates() {
  const dates = [...new Set(this.rows.map(r => r.pri_date))]
  return dates.sort((a,b) => new Date(a) - new Date(b))
},

in this example, rows was just a json object

What I actually need is to set a table header of dates regardless of the dates in the object. I need the first column to be today's date, and then the following 7 days.

So right now it would look like:

2021-11-02 | 2021-11-03 | 2021-11-04 | 2021-11-05 |2021-11-06 | 2021-11-07 | 2021-11-08 | 2021-11-09

How can I change the above code to be a dynamic date range based on the current day's date (trying to keep the format and return the same in my code)

Geoff_S
  • 4,917
  • 7
  • 43
  • 133
  • Assuming you mean `rows` is an array of objects, not a non-existent "json object". – Heretic Monkey Nov 02 '21 at 14:34
  • correct, sorry: It is an array of objects – Geoff_S Nov 02 '21 at 14:35
  • Does this answer your question? [Javascript - get array of dates between 2 dates](https://stackoverflow.com/questions/4413590/javascript-get-array-of-dates-between-2-dates) – Heretic Monkey Nov 02 '21 at 14:36
  • If you need the dates in a specific format, `map` them using [How to format a JavaScript date](https://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date) – Heretic Monkey Nov 02 '21 at 14:38
  • Does this answer your question? [How to format a JavaScript date](https://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date) – derpirscher Nov 02 '21 at 14:38

1 Answers1

1

Try this:

const today = Date.now();
const dates = [
  ...this.rows.map((x,i) => {
    return new Date(today + 86400000 * i).toISOString().substring(0,10);
    // 86400000 ms is 24hours
  })
]
SoGoddamnUgly
  • 706
  • 4
  • 9