Kosal Ang
Thu Nov 24 2022
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
Creating a beautiful menu bar using HTML, CSS, and JavaScript involves creating the structure with HTML
JavaScript uses asynchronous programming. Promises and `async`/`await` are two powerful features that facilitate asynchronous operations in JavaScript
JavaScript's closures and scope are fundamental concepts that significantly impact how code behaves
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
Arrays and objects in JavaScript is fundamental for managing and manipulating data
ECMAScript 6, also known as ES6 or ECMAScript 2015, introduced several powerful features that significantly enhanced the capabilities of JavaScript
Generate all permutations of string and filter unique string by using JavaScript
JavaScript is a versatile scripting language primarily used for client-side web development. It enables interactivity and dynamic content on web pages.
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.
TailwindCSS is a popular CSS framework that can help you create nice user interfaces quickly with pre-made CSS classes.