JavaScript - get the first day of the week from the current date

I need the fastest way to get the first day of the week. For example, today is November 11th and Thursday, I need to convert this date to November 8th (Monday). I need the fastest way for the MongoDB map function, any ideas?

+84
javascript algorithm
Nov 11 2018-10-11
source share
10 answers

Using the getDay method of Date objects, you can find out the number of days of the week (0 = Sunday, 1 = Monday, etc.).

Then you can subtract this number of days plus one, for example:

 function getMonday(d) { d = new Date(d); var day = d.getDay(), diff = d.getDate() - day + (day == 0 ? -6:1); // adjust when day is sunday return new Date(d.setDate(diff)); } getMonday(new Date()); // Mon Nov 08 2010 
+177
Nov 11 '10 at 16:13
source share

Not sure how it compares for performance, but it works.

 var today = new Date(); var day = today.getDay() || 7; // Get current day number, converting Sun. to 7 if( day !== 1 ) // Only manipulate the date if it isn't Mon. today.setHours(-24 * (day - 1)); // Set the hours to day number minus 1 // multiplied by negative 24 alert(today); // will be Monday 

Or as a function:

 function getMonday( date ) { var day = date.getDay() || 7; if( day !== 1 ) date.setHours(-24 * (day - 1)); return date; } getMonday(new Date()); 
+34
Nov 11 '10 at 16:19
source share

Check out Date.js

 Date.today().previous().monday() 
+13
Nov 11 '10 at 16:09
source share

I use this

 function get_next_week_start() { var now = new Date(); var next_week_start = new Date(now.getFullYear(), now.getMonth(), now.getDate()+(8 - now.getDay())); return next_week_start; } 
+3
Jul 24 '14 at 9:03
source share

Check out: moment.js

Example:

 moment().day(-7); // last Sunday (0 - 7) moment().day(7); // next Sunday (0 + 7) moment().day(10); // next Wednesday (3 + 7) moment().day(24); // 3 Wednesdays from now (3 + 7 + 7 + 7) 

Bonus: works with node.js too

+2
Mar 17 '14 at 6:18
source share

This function uses the current millisecond time to subtract the current week, and then subtracts another week if the current date is Monday (the javascript number is calculated from Sunday).

 function getMonday(fromDate) { // length of one day i milliseconds var dayLength = 24 * 60 * 60 * 1000; // Get the current date (without time) var currentDate = new Date(fromDate.getFullYear(), fromDate.getMonth(), fromDate.getDate()); // Get the current date millisecond for this week var currentWeekDayMillisecond = ((currentDate.getDay()) * dayLength); // subtract the current date with the current date millisecond for this week var monday = new Date(currentDate.getTime() - currentWeekDayMillisecond + dayLength); if (monday > currentDate) { // It is sunday, so we need to go back further monday = new Date(monday.getTime() - (dayLength * 7)); } return monday; } 

I tested it when a week passes after a month (as well as years), and it seems to work correctly.

+2
May 19 '16 at 21:13
source share

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,

+2
Jun 03 '16 at 19:09
source share

setDate () has problems with month boundaries, which are noted in the comments above. A clean workaround is to find the date difference using era timestamps, rather than (surprisingly contradictory) methods for the Date object. I.e.

 function getPreviousMonday(fromDate) { var dayMillisecs = 24 * 60 * 60 * 1000; // Get Date object truncated to date. var d = new Date(new Date(fromDate || Date()).toISOString().slice(0, 10)); // If today is Sunday (day 0) subtract an extra 7 days. var dayDiff = d.getDay() === 0 ? 7 : 0; // Get date diff in millisecs to avoid setDate() bugs with month boundaries. var mondayMillisecs = d.getTime() - (d.getDay() + dayDiff) * dayMillisecs; // Return date as YYYY-MM-DD string. return new Date(mondayMillisecs).toISOString().slice(0, 10); } 
+1
Jun 02 '16 at 23:51
source share

Here is my solution:

 function getWeekDates(){ var day_milliseconds = 24*60*60*1000; var dates = []; var current_date = new Date(); var monday = new Date(current_date.getTime()-(current_date.getDay()-1)*day_milliseconds); var sunday = new Date(monday.getTime()+6*day_milliseconds); dates.push(monday); for(var i = 1; i < 6; i++){ dates.push(new Date(monday.getTime()+i*day_milliseconds)); } dates.push(sunday); return dates; } 

Now you can select the date by the index of the returned array.

0
Jul 28 '16 at 12:58
source share
 var dt = new Date() //current date of week var currentWeekDay = dt.getDay(); var lessDays = currentWeekDay == 0 ? 6 : currentWeekDay-1 var wkStart = new Date(new Date(dt).setDate(dt.getDate()- lessDays)); var wkEnd = new Date(new Date(wkStart).setDate(weekStartDate.getDate()+6)); 

This will work well.

0
May 03 '17 at 5:29
source share



All Articles