Get the name of the day

The script on this page shows how to the name of the day with JavaScript. Basically, we use the getDay() method; the method returns an integer between 0 and 6 (0 corresponds to Sunday, 1 corresponds to Monday, and so on.). Because the method returns only a numerical value, we need to associate that with the names of the days in a week. To do that, we use an array called weekDaysArr. In the array, for example, weekDaysArr[0] stores the day name "Sunday", weekDaysArr[1] stores the day name "Monday", and so on.

For your convenience the script is placed inside a function; so you only have to call the function to get the current day. See the code below for details.

The output of the script:

Scripting code

Step 1: place the following code in the head section (between <head> and </head>) of your web document.

<script language="javascript">
function GetDayName () {
var dateObj = new Date();
// create an array
var weekDaysArr = new Array();
// store the current day value (between 0 and 6)
var currDay = dateObj.getDay();
weekDaysArr[0] = "Sunday";
weekDaysArr[1] = "Monday";
weekDaysArr[2] = "Tuesday";
weekDaysArr[3] = "Wednesday";
weekDaysArr[4] = "Thursday";
weekDaysArr[5] = "Friday";
weekDaysArr[6] = "Saturday";
// return the name of the day
return weekDaysArr[currDay];

Step 2: Call the GetDayName () function from your web page:

<script language="javascript" type="text/javascript">
document.write ("Today is " + GetDayName());