For easier date math I’d recommend using a library like moment.js:
# Exactly 7 days ago
> moment().subtract(7,'days')
Moment<2021-08-15T11:02:26+10:00>
# Start of day 7 days ago (local timezone)
> moment().subtract
(7,'days').startOf('day')
Moment<2021-08-15T00:00:00+10:00>
# Start of day 7 days ago (UTC)
> moment().subtract(7,'days').startOf('day').utc()
Moment<2021-08-14T14:00:00Z>
I need to add a .toDate() method at the end to retrieve data from MongoDB, but when .toDate() changes from T00:00 to T07:00 for startOf(‘day’) (which is today). Shouldnt it be T00:00 ?
Sorry, I forgot to include that in my examples. You will need toDate() to convert the Moment object to a JavaScript Date.
If you calculate start of day for local time and convert that to a JavaScript Date with toDate(), you’ll end up with a Date object in UTC which will be offset per your local timezone.