CamKode

Get Monday and Sunday of the Week in JavaScript

Avatar of Kosal Ang

Kosal Ang

Thu Nov 24 2022

Get Monday and Sunday of the Week in JavaScript

Photo by: Jamie Street

To get the Monday and Sunday of the week, use the setDate() method to set the day of the month of a date to the Monday and Sunday of the week and pass the result to the Date() constructor.

1const today = new Date()
2const day = today.getDay()
3const diff = today.getDate() - day + (day === 0 ? -6 : 1)
4const monday = new Date(today.setDate(diff))
5
6const sunday = new Date(monday)
7sunday.setDate(sunday.getDate() + 6)
8
9console.log(monday) // Monday November 21 2022
10console.log(sunday) // Sunday November 27 2022
11

The getDate() method returns the day of the month (1 to 31) of a date.

The getDay() method returns 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.

1function getMonday(d) {
2  date = new Date(d)
3  const day = date.getDay()
4  const diff = date.getDate() - day + (day === 0 ? -6 : 1)
5  return new Date(d.setDate(diff))
6}
7
8function getThisWeek() {
9  const monday = getMonday(new Date())
10  const sunday = new Date(monday)
11  sunday.setDate(sunday.getDate() + 6)
12  return { monday, sunday }
13}
14
15const { monday, sunday } = getThisWeek()
16console.log(monday) // Monday November 21 2022
17console.log(sunday) // Sunday November 27 2022
18

Related Posts

Create a Responsive Navigation Bar using HTML, CSS, and JavaScript

Create a Responsive Navigation Bar using HTML, CSS, and JavaScript

Creating a beautiful menu bar using HTML, CSS, and JavaScript involves creating the structure with HTML

Asynchronous JavaScript: Promises and Async/Await Explained

Asynchronous JavaScript: Promises and Async/Await Explained

JavaScript uses asynchronous programming. Promises and `async`/`await` are two powerful features that facilitate asynchronous operations in JavaScript

Understanding Closures and Scope in JavaScript

Understanding Closures and Scope in JavaScript

JavaScript's closures and scope are fundamental concepts that significantly impact how code behaves

Creating a Responsive Sidebar with Icons and Expandable Menus

Creating a Responsive Sidebar with Icons and Expandable Menus

If you're looking to build a responsive sidebar with icons and expandable menus, this HTML and CSS template can serve as a great starting point

Working with Arrays and Objects in JavaScript

Working with Arrays and Objects in JavaScript

Arrays and objects in JavaScript is fundamental for managing and manipulating data

ES6 Features: Arrow Functions, Destructuring, and Spread Operators

ES6 Features: Arrow Functions, Destructuring, and Spread Operators

ECMAScript 6, also known as ES6 or ECMAScript 2015, introduced several powerful features that significantly enhanced the capabilities of JavaScript

Find all Permutations in JavaScript

Find all Permutations in JavaScript

Generate all permutations of string and filter unique string by using JavaScript

JavaScript Fundamentals: Understanding the Basics

JavaScript Fundamentals: Understanding the Basics

JavaScript is a versatile scripting language primarily used for client-side web development. It enables interactivity and dynamic content on web pages.

Close Dropdown Menu on Click Outside

Close Dropdown Menu on Click Outside

Dropdowns are a feature common to many websites. It's very useful, as they make it easy to show additional data only when it is needed.

How to install Tailwind CSS in Next.js

How to install Tailwind CSS in Next.js

TailwindCSS is a popular CSS framework that can help you create nice user interfaces quickly with pre-made CSS classes.

© 2024 CamKode. All rights reserved

FacebookTwitterYouTube