JavaScript: How to get the day of the week.

In this guide, we will show you how to get the day of the week using JavaScript.

For example, what if you need to get a textual name such as “Monday”, “Tuesday”, or “Wednesday”, etc.?

To do this, we will create a simple function that you can copy and paste into your project.

How to get the day of the week using JavaScript.

Below, we’ve created a custom JavaScript function that will return the day of the week in a text format:

 * Function takes in a Date object and returns the day of the week in a text format.
function getWeekDay(date){
    //Create an array containing each day, starting with Sunday.
    var weekdays = new Array(
        "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
    //Use the getDay() method to get the day.
    var day = date.getDay();
    //Return the element that corresponds to that index.
    return weekdays[day];

In the function above:

  • We created an array containing each day of the week. Note that this array must start with Sunday. If you change the order of this array, the results will be incorrect.
  • We used theĀ Date.prototype.getDay() method to get the day of the week. This method returns a numeric value that is between 0 and 6. The number 0 will always be a Sunday and the number 6 will always be a Saturday.
  • Finally, we returned the array element that corresponds to that numeric value. In other words, if getDay() returns 1, then 1 will correspond to the “Monday” value in our array.

Take a look at the following examples:

//The current weekday in a text format.
var date = new Date();
var weekDay = getWeekDay(date);
console.log('The current weekday is ' + weekDay);

//Finding out what day a specific date fell on.
var date = new Date('December 25, 1987');
var weekDay = getWeekDay(date);
console.log('Christmas Day in 1987 fell on a ' + weekDay);

//What weekday is tomorrow?
var tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 1);
var weekDay = getWeekDay(tomorrow);
console.log('Tomorrow will be a ' + weekDay);

As you can see, you must provide this function with the JavaScript Date object.

In the examples above, we were able to:

  1. Figure out the current day of the week.
  2. Determine what day a specific date falls on.
  3. Figure out what day tomorrow is.

If you are looking for the three-letter version of each day, then you can simply modify the array in the getWeekDay function.