0

I have a unique problem. I am allowing users to set the start of the week as any day. For the code it can be text as "Tuesday" or a number from 0-6. I want to get 2 unix timestamps of the last full week from their chosen start date. So it's Monday now. If they chose Thursday as their start week I want a value of 12am 10 days ago and another for 3 days ago.

So i have this

      //Day club week starts on
      let startday = new Date(res.data.payload[0].startday * 1000).getDay() 
      let todaysDay = new Date().getDay() 
      let dayOffset = todaysDay - startday
      if (startday > todaysDay) {
        dayOffset = todaysDay + (7 - startday)
      }

      var endWeek = new Date()
      endWeek.setDate(endWeek.getDate() - dayOffset)

      var startWeek = new Date()
      endWeek.setDate(endWeek.getDate() - (dayOffset + 7))

But I don't know how to create a new date X days ago at 12am.

Is this on the right track? Thanks

MomasVII
  • 4,641
  • 5
  • 35
  • 52
  • Instead of reinventing the wheel, I would use a library such as [Day.js](https://day.js.org/), then you can code something like `dayjs(startday).subtract(7, 'days').startOf('day')` – Lil Devil Mar 22 '21 at 02:43
  • There are [many duplicates for adding days to a date](https://stackoverflow.com/search?q=%5Bjavascript%5D+add+days+to+a+date), e.g. [*Add days to JavaScript Date*](https://stackoverflow.com/questions/563406/add-days-to-javascript-date). Setting it to 12 am (i.e. 00:00:00) uses [*setHours*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setHours): `date.setHours(0,0,0,0)`. – RobG Mar 22 '21 at 03:12

0 Answers0