12

I am using 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 (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));

This works fine if last Monday and Sunday were also in the same month, but I just noticed today that it doesn't work if today is December and last Monday was in November.

I'm a total JS novice, is there another way to get these dates?

user18577
  • 643
  • 3
  • 9
  • 21

9 Answers9

31

You can get the previous Monday by getting the Monday of this week and subtracting 7 days. The Sunday will be one day before that, 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);

For 2012-12-03 I get:

Mon 26 Nov 2012
Sun 25 Nov 2012

Is that 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 add and subtracting years, months and days. The object will handle negative values automatically, e.g.

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

Will create a date for 2012-11-30 (noting that months are zero based so 11 is December). Also:

d.setMonth(d.getMonth() - 1); // 2012-10-30

d.setDate(d.getDate() - 30);  // 2012-09-30
RobG
  • 142,382
  • 31
  • 172
  • 209
7

if you dont want to do it with an external library you should work with timestamps. i created a solution where you would substract 60*60*24*7*1000 (which is 604800000, which is 1 week in milliseconds) from the current Date and go 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));
hereandnow78
  • 14,094
  • 8
  • 42
  • 48
  • 3
    Does not work in countries that use DST, because one week is not always 60*60*24*7*1000ms. For example at 2014-03-31 00:30:00 in France, lastMonday would be 2014-03-17 even though March 24th and March 31st are Mondays. – personne3000 Feb 22 '14 at 18:17
4

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

Toni Toni Chopper
  • 1,833
  • 2
  • 20
  • 29
  • 1
    can you explain how, other than just linking to moment? – chovy Dec 27 '15 at 09:16
  • 1
    moment().day(-6) to get last monday and moment().day(-1) to get last sunday (where monday is the first day of the week and sunday is the last otherwise moment().day(-7) to get last sunday) – Toni Toni Chopper Dec 29 '15 at 08:29
3

A few 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 a week day, so it doesn't matter what day is it today, only week matters.

Alex Lapa
  • 1,149
  • 11
  • 21
2

This is the general solution of find any day of any week.

function getParticularDayTimestamp(lastWeekDay) {
  var currentWeekMonday = new Date().getDate() - new Date().getDay() + 1;
  return new Date().setDate(currentWeekMonday - lastWeekDay);
}

console.log(getParticularDayTimestamp(7)) // for last week monday
console.log(getParticularDayTimestamp(1)) // for last week sunday
console.log(getParticularDayTimestamp(14)) // for last to last week monday
console.log(getParticularDayTimestamp(8)) // for last to last week sunday
Aman Jain
  • 2,294
  • 1
  • 16
  • 22
1

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

it can be this simple.

var today = new Date();
var sunday = new Date(this.today.getFullYear(), this.today.getMonth(), this.today.getDate() - this.today.getDay());
Jimmy Kane
  • 16,223
  • 11
  • 86
  • 117
0

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 next post: https://stackoverflow.com/a/728694/6751764

Community
  • 1
  • 1
urpi5
  • 117
  • 1
  • 7
0

You can use a third party date library to deal with dates. For example:

var startOfWeek = moment().startOf('week').toDate();
var endOfWeek   = moment().endOf('week').toDate();

if you want to use JavaScript then use the below code

var curr = new Date; // get current date
      var first = curr.getDate() - curr.getDay()+1; // First day is the day of the month - the day of the week
      var last = first + 6; // last day is the first day + 6
      var startDate = new Date(curr.setDate(first));
      startDate = ""+startDate.getFullYear()+"-"+ (startDate.getMonth() + 1) + "-" + startDate.getDate() 

      var endDate = new Date(curr.setDate(last));
      endDate = "" + (endDate.getMonth() + 1) + "/" + endDate.getDate() + "/" + endDate.getFullYear();
      
       alert(startDate+" ,   "+endDate)
Deepak Singh
  • 749
  • 4
  • 16