JavaScript: Get textual day of week.

This is a short JavaScript guide on how to get the full textual representation of the day of the week. i.e. How to get “Monday”, “Tuesday” or “Wednesday” in JavaScript.

Custom function.

Below, I’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, you will get incorrect results.
  • 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. 0 is considered to be a Sunday and 6 is considered to be Saturday.
  • Finally, we returned the array element that corresponds to that numeric value. i.e. If getDay() returns 1, then 1 will correspond to “Monday” in our array. As a result, we must make sure that each element in the array is in the correct order.

Here are a few examples of this function being used:

//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 samples above, we were able to:

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

PS: If you’re looking for the three-letter version of each day, then simply modify the array in theĀ getWeekDay function.

Hopefully, you found this function to be useful!