How to get period from last week’s Monday to last week’s Sunday as a date using JavaScript
I tried to find information in different sources, but did not find an answer, I hope they will help me here
How to get period from last week’s Monday to last week’s Sunday as a date using JavaScript
I tried to find information in different sources, but did not find an answer, I hope they will help me here
2
Answers
Assuming timezone is not an issue, you can make use of the
Date.prototype.setDate()
to first get an anchor to last week, then usegetDay
to know which day you are in, offset that by your anchor and you can obtain the Monday and Sunday:First let us find the day of today.
Then we’ll find find what day it is.
The above will return the day of the week where Sunday = 0 and Saturday = 6.
With this we can go back until last Sunday by subtracting the day of the week from the day.
And then to get the last monday, we just subtract 6 days from that date.
Then we can convert the dates to strings
In all it would look like this