Introduction

How do you get the full name of the current weekday using vanilla JavaScript?

You can get that information by using the JavaScript Date object along with two different methods: date.getDay() and date.toLocaleString().

In this article, we'll show you both of those methods.

Let's get started!

Table of Contents

Method 1 — toLocaleString()

The first method we'll cover is the toLocaleString() function. This is a built-in function that we'll use to convert a Date object into a string that represents the current day of the week.

Here's what the code looks like:

    
      var dateObj = new Date()
      var weekday = dateObj.toLocaleString("default", { weekday: "long" })
      // weekday = "Saturday"
    
  

The first thing we do is create a dateObj variable that holds a Date object for the current date.

Using that dateObj, we can use the toLocaleString() function with two arguments. The first argument is a language tag that we set to "default". And the second argument is an object with options data in it. In that object, we tell the function to return the weekday date component in the "long" formated style.

If you logged the weekday variable, the full name of the current day of the week would be displayed.

You can also get the short weekday format ("Sun", "Mon", "Tue", etc.) by modifying your toLocaleString() function to use the "short" formatting style:

    
      var dateObj = new Date()
      var weekday = dateObj.toLocaleString("default", { weekday: "short" })
      // weekday = "Sat"
    
  

As you can see, the short version of the weekday name is returned.

We'll show you one more additional method in the next section.

Method 2 - getDay()

The second method is the getDay() function. This will return the weekday of a specified date as a zero-based value (where 0 indicates the first day of the week as a Sunday).

Here's what the code looks like to get the zero-based value:

    
      var dateObj = new Date()
      var weekdayNumber = dateObj.getDay()
      // weekdayNumber = 6
    
  

The first thing we do is create a new Date object with the new Date() method and store it in a variable named dateObj.

Then, we use the getDay() function to get the zero-based number value of the current weekday.

If you console.log() the weekdayNumber variable, a digit representing the current weekday will be returned.

To get the full name of the weekday, we need to create an array of day strings that correspond to the weekdayNumber digit value. Using that array, we can get the full name of the weekday.

Here's what the updated code looks like:

    
      var arrayOfWeekdays = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]

      var dateObj = new Date()
      var weekdayNumber = dateObj.getDay()
      var weekdayName = arrayOfWeekdays[weekdayNumber]
      // weekdayName = "Saturday"
    
  

We added a arrayOfWeekdays array of strings with each day name in it.

Then, we used the weekdayNumber as the index number in the arrayOfWeekdays array to get the string value of the current day.

If you console.log() the weekdayName variable, a full day name will be returned.

Conclusion

In this article, we showed you two different methods to get the full name of the current weekday using vanilla JavaScript: toLocaleString() and getDay().

Thanks for reading and happy coding!