• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • VBA
  • About
You are here: Home / JavaScript / How in JavaScript to Get the Day of the Week

How in JavaScript to Get the Day of the Week

May 1, 2022 Leave a Comment

In JavaScript, we can get the day of the week using the getDay() method. This will return a number from 0-6, with 0 being Sunday, and 6 being Saturday.

var currDate = new Date();
var currDayOfWeek = currDate.getDay();

The value of currDayOfWeek in the code above would return whatever the day of the week it currently is. However this value is just a number, and most of the time we will want the actual name of the day of the week. So we can convert the number to the day it corresponds to using some simple code below.

We will make an array of day name and convert our number to be a day name using it.

var currDate = new Date();
var daysOfWeek = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
var dayOfWeekNumber = currDate.getDay();
var nameOfDay = daysOfWeek[dayOfWeekNumber];

The date this post was written was May 1st on a Sunday, so Sunday would be the value of nameOfDay in the code above.

Getting and Displaying the Current Day, Month, and Year with the Help of the getDate method in JavaScript

Below we will provide code to get the current date in JavaScript, and let the user see it in a friendly format when they click a button.

<div id="div1">
  <div id="click-me" onclick="genNewDate()">Get Date</div>
  <p>Today's Date is: <span id="theDate"></span></p>
</div>

We will first get the current date using new Date(). Once we have the date object, we can then get the day using the getDate() method, the year using the getFullYear() method, and the month using the getMonth() method.

We will also add what day of the week it is as well using the getDay() method.

We can make the date look even better by converting the month number into the month as a String. We just need to make an array with all the months to make this happen, which we will do below.

We will also do this for the day of the week as we did in the above example.

function genNewDate(){
  var currDate = new Date();
  var currDay = currDate.getDate();
  var currDayOfWeek = currDate.getDay();
  var currMonth = currDate.getMonth();
  var currYear = currDate.getFullYear();
  var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
  var monthName = months[currMonth];
  var daysOfWeek = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
  var dayName = daysOfWeek[currDayOfWeek];
  document.getElementById("theDate").innerHTML = dayName + ", " + monthName + ' ' + currDay + ', ' + currYear;
};

The final code and output for this example is below:

Code Output:

Get Date

Today’s Date is:

Full Code:

<div id="div1">
  <div id="click-me" onclick="genNewDate()">Get Date</div>
  <p>Today's Date is: <span id="theDate"></span></p>
</div>

<script>

function genNewDate(){
  var currDate = new Date();
  var currDay = currDate.getDate();
  var currDayOfWeek = currDate.getDay();
  var currMonth = currDate.getMonth();
  var currYear = currDate.getFullYear();
  var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
  var monthName = months[currMonth];
  var daysOfWeek = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
  var dayName = daysOfWeek[currDayOfWeek];
  document.getElementById("theDate").innerHTML = dayName + ", " + monthName + ' ' + currDay + ', ' + currYear;
};

</script>

Hopefully this article has been useful in helping you understand how in JavaScript to get the day of the week.

Other Articles You'll Also Like:

  • 1.  JavaScript Check If Number is a Whole Number
  • 2.  Using JavaScript to Change the href of a Link
  • 3.  Set the Height of a Div Using JavaScript
  • 4.  How to Remove Non Numbers From String in JavaScript
  • 5.  lastIndexOf JavaScript – Get the Last Position of a String in Another String
  • 6.  JavaScript substring – How to Get a Substring From a String
  • 7.  JavaScript Ceiling – Using Math.ceil() Method to Round Up to Ceiling
  • 8.  Using JavaScript to Rotate an Image
  • 9.  Remove Undefined Values From an Array in JavaScript
  • 10.  Using JavaScript to Add Leading Zeros

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2022 · The Programming Expert · About · Privacy Policy