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:

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:

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!

Facebook Comments