Write a JavaScript function to get the week start date

How to get the start date and end date of current week, with the week start from Monday and end with Sunday, with JavaScript?

closeup photo of yellow Labrador retriever puppy

Sometimes, we want to get the start date and end date of current week, with the week start from Monday and end with Sunday, with JavaScript.

In this article, we’ll look at how to get the start date and end date of current week, with the week start from Monday and end with Sunday, with JavaScript.

How to get the start date and end date of current week, with the week start from Monday and end with Sunday, with JavaScript?

To get the start date and end date of current week, with the week start from Monday and end with Sunday, with JavaScript, we can use some date methods.

const startAndEndOfWeek = (date) => < const now = date ? new Date(date) : new Date().setHours(0, 0, 0, 0); const monday = new Date(now); monday.setDate(monday.getDate() - monday.getDay() + 1); const sunday = new Date(now); sunday.setDate(sunday.getDate() - sunday.getDay() + 7); return [monday, sunday]; >const d = new Date(2022, 3, 1) console.log(startAndEndOfWeek(d)) 

to define the startAndEndOfWeek function that takes a date .

In it, we compute the Monday and Sunday of the week that has the given date .

Читайте также:  Php fopen as root

To do this, we create a copy of date with the Date constructor.

Then we get monday by creating a new date from now .

And then we call setDate with monday.getDate() — monday.getDay() + 1 to set the day of the week of monday to the Monday within the same week as date .

Likewise, we call setDate with sunday.getDate() — sunday.getDay() + 7 to set the day of the week of sunday to the Sunday within the same week as date .

Finally, we return both dates.

As a result, the console log logs [Mon Mar 28 2022 00:00:00 GMT-0700 (Pacific Daylight Time), Sun Apr 03 2022 00:00:00 GMT-0700 (Pacific Daylight Time)] .

Conclusion

To get the start date and end date of current week, with the week start from Monday and end with Sunday, with JavaScript, we can use some date methods.

Источник

JavaScript: Get the week start date

Write a JavaScript function to get the week’s start date.

Sample Solution:-

        

JavaScript Code:

function startOfWeek(date) < var diff = date.getDate() - date.getDay() + (date.getDay() === 0 ? -6 : 1); return new Date(date.setDate(diff)); >dt = new Date(); console.log(startOfWeek(dt).toString()); 
Mon Jun 18 2018 18:22:59 GMT+0530 (India Standard Time)

Flowchart: JavaScript- Get the week start date

Improve this sample solution and post your code through Disqus

What is the difficulty level of this exercise?

Test your Programming skills with w3resource’s quiz.

Follow us on Facebook and Twitter for latest update.

JavaScript: Tips of the Day

The unary plus tries to convert an operand to a number. true is 1, and false is 0.

The string ‘Lydia’ is a truthy value. What we’re actually asking, is «is this truthy value falsy?». This returns false.

  • Weekly Trends
  • Java Basic Programming Exercises
  • SQL Subqueries
  • Adventureworks Database Exercises
  • C# Sharp Basic Exercises
  • SQL COUNT() with distinct
  • JavaScript String Exercises
  • JavaScript HTML Form Validation
  • Java Collection Exercises
  • SQL COUNT() function
  • SQL Inner Join
  • JavaScript functions Exercises
  • Python Tutorial
  • Python Array Exercises
  • SQL Cross Join
  • C# Sharp Array Exercises

We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook

Источник

Оцените статью