Javascript: get Monday and Sunday of the previous week

I use the following script to get Monday (first) and Sunday (last) for the previous week:

var curr = new Date; // get current date var first = curr.getDate() - curr.getDay() - 6; // Gets day of the month (eg 21) - the day of the week (eg 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)); 

This works great if last Monday and Sunday were also the same month, but today I noticed that it does not work if today is December and the last Monday was in November.

I'm a complete JS newbie, is there any other way to get these dates?

+6
source share
7 answers

If you do not want to do this using an external library, you must work with timestamps. I created a solution in which you would subtract 60 * 60 * 24 * 7 * 1000 (which is 604800000, which is 1 week in milliseconds) from the current date and from there:

 var beforeOneWeek = new Date(new Date().getTime() - 60 * 60 * 24 * 7 * 1000) , day = beforeOneWeek.getDay() , diffToMonday = beforeOneWeek.getDate() - day + (day === 0 ? -6 : 1) , lastMonday = new Date(beforeOneWeek.setDate(diffToMonday)) , lastSunday = new Date(beforeOneWeek.setDate(diffToMonday + 6)); 
+5
source

You can get the previous Monday by getting the Monday of this week and subtracting 7 days. Sunday will be the day before, so:

 var d = new Date(); // set to Monday of this week d.setDate(d.getDate() - (d.getDay() + 6) % 7); // set to previous Monday d.setDate(d.getDate() - 7); // create new date of day before var sunday = new Date(d.getFullYear(), d.getMonth(), d.getDate() - 1); 

On 2012-12-03 I get:

 Mon 26 Nov 2012 Sun 25 Nov 2012 

Is this what you want?

 // Or new date for the following Sunday var sunday = new Date(d.getFullYear(), d.getMonth(), d.getDate() + 6); 

which gives

 Sun 02 Dec 2012 

In general, you can manipulate date objects by adding and subtracting years, months, and days. The object will process negative values ​​automatically, for example.

 var d = new Date(2012,11,0) 

A date will be created for 2012-11-30 (noting that the months are zero, so 11 is December). Also:

 d.setMonth(d.getMonth() - 1); // 2012-10-30 d.setDate(d.getDate() - 30); // 2012-09-30 
+11
source

You can use a library like moment.js. See the subtract method http://momentjs.com/docs/#/manipulating/subtract/

+4
source

Several answers mentioned moment, but no one wrote about this simple method:

 moment().day(-13) // Monday last week moment().day(-7) // Sunday last week 

.day sets the weekly day, so it doesn't matter what day it is today, only the week matters.

+3
source

Using Moment, you can do the following

  var lastWeek = moment().isoWeek(moment().subtract(1,'w').week()); var mondayDifference = lastWeek.dayOfYear() - lastWeek.weekday() + 1; var sundayDifference = mondayDifference - 1; var lastMonday = moment().dayOfYear(mondayDifference); var lastSunday = moment().dayOfYear(sundayDifference ); 
+1
source

it can be that simple.

 var today = new Date(); var sunday = new Date(this.today.getFullYear(), this.today.getMonth(), this.today.getDate() - this.today.getDay()); 
+1
source

Here you have a multi-purpose function:

  function getThe(numOfWeeks, weekday, tense, fromDate) { // for instance: var lastMonday = getThe(1,"Monday","before",new Date()) var targetWeekday = -1; var dateAdjustment = clone(fromDate); var result = clone(fromDate); switch (weekday) { case "Monday": targetWeekday = 8; break; case "Tuesday": targetWeekday = 2; break; case "Wednesday": targetWeekday = 3; break; case "Thursday": targetWeekday = 4; break; case "Friday": targetWeekday = 5; break; case "Saturday": targetWeekday = 6; break; case "Sunday": targetWeekday = 7; } var adjustment = 7 * (numOfWeeks - 1); if (tense == "after") adjustment = -7 * numOfWeeks; dateAdjustment.setDate(fromDate.getDate() - targetWeekday); var weekday = dateAdjustment.getDay(); result.setDate(fromDate.getDate() - weekday - adjustment); result.setHours(0,0,0,0); return result; } 

You can find the clone (obj) function in the following post: fooobar.com/questions/439 / ...

0
source

All Articles