我正在使用以下脚本来获取前一周的星期一(第一)和星期日(最后):
var curr = new Date; // get current date
var first = curr.getDate() - curr.getDay() - 6; // Gets day of the month (e.g. 21) - the day of the week (e.g. wednesday = 3) = Sunday (18th) - 6
var last = first + 6; // last day is the first day + 6
var startDate = new Date(curr.setDate(first));
var endDate = new Date(curr.setDate(last));
如果上周一和周日也在同一个月,这很好用,但我今天才注意到,如果今天是 12 月而上周一是 11 月,它就行不通了。
我是个 JS 新手,还有其他方法可以获取这些日期吗?