How To Get First And Last Day Of The Current Week Using JavaScript?

How To Get First And Last Day Of The Current Week Using JavaScript?

When we work with Date in JavaScript in some cases you need to get the first and last day of the current week. In this article we’ll discuss about 3 simple ways to do it. Read on to learn more.

Get first and last day of the current week using JavaScript

Here we will show you 3 ways to get the first and last day of the current week using JavaScript.

Notice: This tutorial applies to the first day of the week is Sunday, and the weekend is Saturday.

Using getDay(), getDate(), setDate() functions

The getDay() syntax:

object.getDay()

Description: Get object‘s day of the week (Range: 0 – 6).

The getDate() syntax:

object.getDate()

Description: Get the day of the month of object (Range: 1 – 31).

The setDate() syntax:

object.setDate()

Description: Set the day of the month for object (Range: 1 – 31).

First, we will create a date object using the new Date().

Then we use the getDate() function to get the current date. Continuing, we use the getDay() function to get the day of the week (0 – 6). The first day of the week is calculated using the algorithm:

First day of week = Day of the month - Day of the week

Next, we easily get the weekend by the formula:

Last day of the week = First day of the week + 6

Finally, we need to pass the numbers that we have calculated into the setDate() function and successfully get the first and last day of the week.

You can use the toLocaleDateString() function to make the display more visible!

See the code below for a better understanding.

let currentDate = new Date();

// First day of week = Day of the month - Day of the week
let first = currentDate.getDate() - currentDate.getDay();

// Last day of the week = First day of the week + 6
let last = first + 6;

// Use setDate() to create first and last days
let firstDay = new Date(currentDate.setDate(first));
let lastDay = new Date(currentDate.setDate(last));

console.log(firstDay.toLocaleDateString());
console.log(lastDay.toLocaleDateString());

Output:

10/9/2022
10/15/2022

Adding a custom function to the Date prototype

This method is also based on the algorithm of the above method, but we will add it to the Date prototype. After that, we just call the function with a line of code when we need to get the result. Look closely at the example below to see how to add a custom function to the prototype.

// Add 'getFirstDay' function to Date Prototype
Date.prototype.getFirstDay = function () {
    let first = this.getDate() - this.getDay(); // this = Date object
    return new Date(this.setDate(first));
};

// Add 'getLastDay' function to Date Prototype
Date.prototype.getLastDay = function () {
    let last = this.getDate() - this.getDay() + 6; // this = Date object
    return new Date(this.setDate(last));
};

let date = new Date();

// Get the first day of the week
console.log(date.getFirstDay().toLocaleDateString());

// Get the last day of the week
console.log(date.getLastDay().toLocaleDateString());

Output:

10/9/2022
10/15/2022

Using moment.js library

This approach is the most straightforward and most convenient of the three ways. But it’s not for people who don’t like importing 3rd party libraries into their projects.

If you are not someone who does not like 3rd libraries, then try this approach!

First, you have to import moment.js library. You can download the library or use the CDN here. In this tutorial, we will use the CDN for convenience.

We use the moment() instead of the new keyword to create an object. Getting the first and last day of the week is also very simple. Use startOf('week') for the first day and endOf('week') for the last day. It is done!

Look at the example below to see how to use moment.js.

// Get the first and last day of the week
let firstDay = moment().startOf('week');
let lastDay = moment().endOf('week');

console.log('First day of the week: ' + firstDay.format('MM/DD/YYYY'));
console.log('Last day of week: ' + lastDay.format('MM/DD/YYYY'));

Output:

First day of the week: 10/09/2022
Last day of week: 10/15/2022

Summary

The above article has shown you how to get first and last day of the current week using JavaScript by using the basic functions in the Date object and the moment.js library. We hope this article will be useful to you. Have a beautiful day!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *