Skip to content
the poor coder

get day name from date javascript

Poor Man
Published
Reading time
2 min read
On this page

How to Get Day Name from Date in JavaScript?#

Getting the day name from a given date in JavaScript is a common task in web development. There are several ways to achieve this, but we'll focus on the most common ones.

Using the toLocaleDateString() Method#

The easiest way to get the day name from a date in JavaScript is to use the toLocaleDateString() method. This method returns a string that represents the date using the browser's locale settings.

JavaScript

      const date = new Date();
      const options = { weekday: 'long' };
      const dayName = date.toLocaleDateString('en-US', options);
      console.log(dayName); // "Tuesday"
    

In the above code, we create a new Date object and then use the toLocaleDateString() method to convert it to a string in the specified format. We pass two arguments to the method: the first one is the locale, which is set to 'en-US' for English language and US region; and the second one is an options object that specifies the format of the output. In this case, we only care about the weekday, so we set the "weekday" property to 'long' to get the full name of the day.

Using the getDay() Method#

Another way to get the day name from a date in JavaScript is to use the getDay() method, which returns the day of the week as a number (0 for Sunday, 1 for Monday, and so on).

JavaScript

      const date = new Date();
      const days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
      const dayName = days[date.getDay()];
      console.log(dayName); // "Tuesday"
    

In the above code, we first create a new Date object and then define an array of day names. We use the getDay() method to get the day of the week as a number and use it to index the array to get the corresponding day name.

Using a Third-Party Library#

If you're working on a project that requires more advanced date manipulation, you may want to consider using a third-party library like Moment.js or date-fns. These libraries provide a wide range of functions and utilities for working with dates and times in JavaScript.

JavaScript

      const date = new Date();
      const dayName = moment(date).format('dddd');
      console.log(dayName); // "Tuesday"
    

In the above code, we use the Moment.js library to format the date object and get the day name in the specified format ('dddd' stands for the full name of the day).

These are just a few ways to get the day name from a date in JavaScript. Depending on your requirements and preferences, you may choose one of these methods or explore other alternatives.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC