次のスクリプトを使用して、前の週の月曜日(最初)と日曜日(最後)を取得しています。
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初心者ですが、これらの日付を取得する別の方法はありますか?