Categories
JavaScript

Get the Monday of the Current week

https://bobbyhadz.com/blog/javascript-get-monday-of-current-week

function getMondayOfCurrentWeek() {
  const today = new Date();
  // Calculate date of the current week
  // getDate() - getDay() => 0-based week start (Sunday as week start)
  // +1 to get date of Monday
  const first = today.getDate() - today.getDay() + 1;

  // Use the setDate() method to get the timestamp of the Monday of the current week
  const monday = new Date(today.setDate(first));
  return monday;
}
  • The getDate method returns an integer between 1 and 31 that represents the day of the month for the given date
  • The getDay method returns an integer between 0 (Sunday) and 6 (Saturday) that represents the day of the week for the date
  • The return value of setDate() is the number of milliseconds between 1 January 1970 00:00:00 UTC and the given date (the Date object is also changed in place)

One reply on “Get the Monday of the Current week”

Leave a comment