Using the getDay method of Date objects, you can know the number of day of the week (being 0=Sunday, 1=Monday, etc).

You can then subtract that 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));
}

console.log( getMonday(new Date()) ); // e.g. Mon Nov 08 2010

Answer from Christian C. Salvadó on Stack Overflow
🌐
W3Resource
w3resource.com › javascript-exercises › javascript-date-exercise-50.php
JavaScript: Get the week start date - w3resource
-6 : 1); // Set the date to the ... ... The function "startOfWeek()" takes a "Date" object 'date' as input. It calculates the difference between the day of the month ('date.getDate()') and the day of the week ('date.getDay()'). If the day of the week is Sunday (0), it adjusts the difference to ensure the start of the week is considered Monday...
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Date › getDay
Date.prototype.getDay() - JavaScript | MDN
The weekday variable has value 1, based on the value of the Date object xmas95, because December 25, 1995 is a Monday. ... const xmas95 = new Date("1995-12-25T23:15:30"); const weekday = xmas95.getDay(); console.log(weekday); // 1
🌐
Medium
medium.com › @quynh.totuan › how-to-get-the-current-week-in-javascript-9e64d45a9a08
How to Get the Current Week in Javascript | by Quynh Nhu To Tuan | Medium
August 29, 2017 - What we need to do is get the day of the month, find out the weekday (Tuesday, Wednesday, etc), subtract the number needed to get to Monday, and then create the new date using the new calculated day and combine it with the current month. We are going to make use of Javascript’s built in methods: new Date, getDate, getDay, and setDate.
🌐
{app_name}/
camkode.com › posts › get-monday-and-sunday-of-the-week-in-javascript
Get Monday and Sunday of the Week in JavaScript
const today = new Date() const ... the day of the week for the specified date according to local time: 0 for Sunday, 1 for Monday, 2 for Tuesday, and so on....
🌐
W3Schools
w3schools.com › jsref › jsref_getday.asp
JavaScript Date getDay() Method
The getDay() method returns the day of the week (0 to 6) of a date. Sunday = 0, Monday = 1, ...
Find elsewhere
🌐
Bobby Hadz
bobbyhadz.com › blog › javascript-get-first-and-last-day-of-current-week
Get First and Last Day of the current Week in JavaScript | bobbyhadz
Copied!const today = new Date(); // ✅ Get the first day of the current week (Monday) function getFirstDayOfWeek(d) { // 👇️ clone date object, so we don't mutate it const date = new Date(d); const day = date.getDay(); // 👉️ get day ...
🌐
Experts Exchange
experts-exchange.com › questions › 28155045 › getting-the-date-of-monday-and-sunday-of-the-current-week.html
Solved: getting the date of monday and sunday of the current week | Experts Exchange
February 15, 2013 - ... Log in or create a free account to see answer. Signing up is free and takes 30 seconds. No credit card required. ... function getMonday(d) { d = new Date(d); var day = d.getDay(), diff = d.getDate() - day + (day == 0 ?
🌐
Javatpoint
javatpoint.com › calculate-current-week-number-in-javascript
Calculate current week number in JavaScript - javatpoint
Calculate current week number in JavaScript with javascript tutorial, introduction, javascript oops, application of javascript, loop, variable, objects, map, typedarray etc.
Top answer
1 of 9
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
2 of 9
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));
🌐
JavaScript in Plain English
javascript.plainenglish.io › how-to-get-the-first-day-of-the-week-from-the-current-date-with-javascript-7743ad44fafd
How to Get the First Day of the Week from the Current Date with JavaScript? | by John Au-Yeung | JavaScript in Plain English
July 31, 2021 - In the function, create a Date instance with the Date constructor. Then we call getDay to get the day of the week. It returns 0 for Sunday, 1 for Monday, and so on up to 6 for Saturday.
🌐
Weeknumber
weeknumber.com › how-to › javascript
Week numbers in JavaScript
Attribution not necessary but appreciated. // Source: https://weeknumber.com/how-to/javascript // Returns the ISO week of the date. Date.prototype.getWeek = function() { var date = new Date(this.getTime()); date.setHours(0, 0, 0, 0); // Thursday in current week decides the year.
🌐
TutorialsPoint
tutorialspoint.com › javascript › date_getday.htm
JavaScript Date getDay() Method
<html> <body> <script> function isWeekend() { const currentDate = new Date('2023-12-23'); const dayOfWeek = currentDate.getDay(); if (dayOfWeek === 0 || dayOfWeek === 6) { document.write("It's the weekend"); } else { document.write("It's a weekday."); } } isWeekend(); </script> </body> </html> The above program prints "It's the weekend" because the day of the week for the specified date is 6.
🌐
GitHub
gist.github.com › dblock › 1081513
get week of the year in JavaScript · GitHub
* * e.g. 2014/12/29 is Monday in week 1 of 2015 * 2012/1/1 is Sunday in week 52 of 2011 */ function getWeekNumber(d) { // Copy date so don't modify original d = new Date(Date.UTC(d.getFullYear(), d.getMonth(), d.getDate())); // Set to nearest Thursday: current date + 4 - current day number // Make Sunday's day number 7 d.setUTCDate(d.getUTCDate() + 4 - (d.getUTCDay()||7)); // Get first day of year var yearStart = new Date(Date.UTC(d.getUTCFullYear(),0,1)); // Calculate full weeks to nearest Thursday var weekNo = Math.ceil(( ( (d - yearStart) / 86400000) + 1)/7); // Return array of year and week number return [d.getUTCFullYear(), weekNo]; }
🌐
Day.js
day.js.org › docs › en › get-set › weekday
Day of Week (Locale Aware) · Day.js
Gets or sets the day of the week according to the locale. ... If the locale assigns Sunday as the first day of the week, dayjs().weekday(0) will be Sunday. If Monday is the first day of the week, dayjs().weekday(0) will be Monday.