JavaScript to Get the Day of the Week

The day of the week can be retrieved in JavaScript by using the getDay method JavaScript’s Date object, as follows:

var dteNow = new Date();
var dteToday = dteNow.getDay();
document.write(dteToday);

The value returned by getDay is between 0 (Sunday) and 6 (Saturday). To convert this to a string representing the day of the week, use a switch statement.

var strDay = '';

switch(dteToday)
{
case 0: strDay = 'Sunday'; break;
case 1: strDay = 'Monday'; break;
case 2: strDay = 'Tuesday'; break;
case 3: strDay = 'Wednesday'; break;
case 4: strDay = 'Thursday'; break;
case 5: strDay = 'Friday'; break;
case 6: strDay = 'Saturday'; break;
}

document.write(strDay);

Related articles:
JavaScript Date Object
JavaScript Switch Case Statement Example

One Response to “JavaScript to Get the Day of the Week”

  1. Thanks a bunch!

Leave a Reply