How to get the day of the week in JavaScript using getDay()?

Is it hard to get the day of the week in JavaScript?

Well, keep reading to discover the easy and simple way of getting the day using the getDay() method.

In this article, you’ll learn how to easily get the day of the week in JavaScript using the getDay() method of the Date object.

What is getDay() method?

The getDay() method is a built-in method of the Date object in JavaScript.

It returns the day of the week for a specified date, according to local time.

The value returned by this method is an integer that represents the day of the week, where 0 represents Sunday, 1 represents Monday, 2 represents Tuesday, and so on up to 6, which represents Saturday.

Syntax

Date.getDay()

Parameter

There is no parameter.

Return value

This method returns the day of the week with (0 to 6) for a specified date.

How to get the day of the week from date in JavaScript?

You can get the day of the week in JavaScrupt using the getDay() method of the Date object.

As what we mentioned a while ago that this method returns an integer between 0 and 6, where 0 represents Sunday and 6 represents Saturday.

Here’s an example of how to use the getDay() method to get the day of the week for a specific date:

const date = new Date("2023-08-02");
const dayOfWeek = date.getDay(); 
console.log(dayOfWeek);

We create a new Date object for August 2, 2023, and then use the getDay() method to get the day of the week for that date.

Output:

3

The method returns 3, that represents the day of week which is “Wednesday.”

You can also use an array of weekday names to get the name of the day of the week instead of just a number:

const weekdaynames = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
const date = new Date("2023-08-02");
const dayOfWeek = date.getDay(); 
console.log(weekdaynames[dayOfWeek]); 

As you have notice, in this example, we use an array of weekday names and index into it using the value returned by the getDay() method to get the name of the day of the week.

Output:

Wednesday

How to get the day of week and the month of the year in JavaScript?

To get the day of the week and the month of the year in JavaScript, you can use the getDay() and getMonth() methods of the Date object, respectively.

The getDay() method returns an integer between 0 and 6, where 0 represents Sunday and 6 represents Saturday. Meanwhile, the getMonth() method returns an integer between 0 and 11, where 0 represents January and 11 represents December.

Here’s an example of how to use these methods to get the day of the week and the month of the year for a specific date:

const date = new Date("2023-08-02");
const dayOfWeek = date.getDay(); 
const monthOfYear = date.getMonth(); 
console.log(dayOfWeek);
console.log(monthOfYear); 

As you can see, we create a new Date object for August 2, 2023, and then we use the getDay() and getMonth() methods to get the day of the week and the month of the year for that date.

Ourput:

3
7

The getDay() method returns 3, which represents “Wednesday,” and the getMonth() method returns 7, which represents August.

You can also use arrays of weekday names and month names to get the names instead of just numbers:

Here’s an example:

const weekdaynames = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
const monthnames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
const date = new Date("2023-08-02");
const dayOfWeek = date.getDay(); ✅
const monthOfYear = date.getMonth(); 
console.log(weekdaynames[dayOfWeek]);
console.log(monthnames[monthOfYear]);

Here, we use arrays of weekday names and month names and index into them using the values returned by the getDay() and getMonth() methods to get the names of the day of the week and the month of the year.

Output:

Wednesday
August

Conclusion

In conclusion, getting the day of the week in JavaScript is not hard at all.

With the getDay() method of the Date object, you can easily get the day of the week for any date.

Apart from that, you can also use arrays of weekday names and month names to get the names of the day of the week and the month of the year.

We are hoping that this article provides you with enough information that help you understand on how to get the day of week in JavaScript.

You can also check out the following article:

Thank you for reading itsourcecoders 😊.

Leave a Comment