Get The First Day Of The Current Week Using JavaScript

Get the first Day of the current Week using JavaScript

Hi guys, today we will learn about how to get the first day of the current week using JavaScript together. If you want to understand it better, follow this article.

Get the first day of the current week using JavaScript

Using getDate() and getDay() methods

Here, we will use the getDate() and getDay() methods to get the first day of the current week. So, if you don’t know how to use syntax or use getDate() and getDay() methods, you can learn about JavaScript here to review.

Firstly, we can use the getDay() method to get the current index of the day stored in the variable day. Using the getDate() method to get the current date of the week stored in the variable date. Then we subtract date from day and add 1 because the day’s index starts from 0 to 6, and you will receive the first day of the current week. Finally, we use the toLocaleDateString() to get the date.

See the code below to better understand.

Code example:

// create a variable to get date today
let get_Date_Today = new Date();

// print index current day by getDay()
console.log('The index of current day is:',get_Date_Today.getDay());

// print current date by getDate()
console.log('The current date is:',get_Date_Today.getDate());

// get first day of the week
let get_First_Day_Of_The_Week = new Date(get_Date_Today.setDate(
get_Date_Today.getDate() - get_Date_Today.getDay() + 1));

// print first day of the week
console.log('The first day of the week is:',
get_First_Day_Of_The_Week.toLocaleDateString());

Output:

The index of current day is: 2
The current date is: 18
The first day of the week is: 10/17/2022

Besides, we can use setDate() to set the day of the week.

Code example:

// create function to get the first day of the week
function get_First_MonDay(d) {

    // create a variable to get date today
    const get_Date_Today = new Date(d);

    // create a variable to get day by getDay()
    const get_Day = get_Date_Today.getDay();

    // calculate and store in a variable get_first
    const get_first = get_Date_Today.getDate() - get_Day + 1;

    // return first day of the week
    return new Date(get_Date_Today.setDate(get_first));
}

// call function and print the first day of the week
console.log('The first day of the week is:',
get_First_MonDay(new Date()).toLocaleDateString());

Output:

The first day of the week: 10/17/2022

Using moment .js library in JavaScript

In addition, we can get the first day of the current week in JavaScript by using moment .js library. It’s very easy to use.

First, you install moment.js library. Then you can easily get the first day of the week by moment.startOf(‘week’). Follow the code example below.

Code example:

// get the first and last day of the week
let get_First_Day = moment().startOf('week');

console.log('The first day of the week: ',
get_First_Day.format('MM/DD/YYYY'));

Output:

The first day of the week: 10/17/2022

Summary

Above are all the simplest ways I offer you to get the first day of the current week using JavaScript. Please leave a comment if you have any questions. I will answer as possible. And so now, see you again!

Maybe you are interested:

Leave a Reply

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