Good evening,
I prefer just having a simple extension method:
Date.prototype.startOfWeek = function (pStartOfWeek) { var mDifference = this.getDay() - pStartOfWeek; if (mDifference < 0) { mDifference += 7; } return new Date(this.addDays(mDifference * -1)); }
You will notice that another extension method that I use is actually used:
Date.prototype.addDays = function (pDays) { var mDate = new Date(this.valueOf()); mDate.setDate(mDate.getDate() + pDays); return mDate; };
Now, if your weeks start on Sunday, go to the "0" parameter for the pStartOfWeek parameter, for example:
var mThisSunday = new Date().startOfWeek(0);
Similarly, if your weeks start on Monday, go to "1" for the pStartOfWeek parameter:
var mThisMonday = new Date().startOfWeek(1);
Hello,
Chandler Zwolle Jun 03 '16 at 19:09 2016-06-03 19:09
source share